Lukasz Czerwinski
Lukasz Czerwinski

Reputation: 15432

How to insert main method in Netbeans (shortcut)

Sometimes you would like to run a single file to test some code quickly. Typing in public static void main(String[] args) { each time is tedious. How to do it quicker?

Upvotes: 23

Views: 48481

Answers (3)

exe2bin
exe2bin

Reputation: 61

"psvm" is not the most intuitive abbreviation I can think of when I want to quickly insert a main method, so I created a new one more to my liking in the Code Templates library.

"main" seemed to be more natural for me, and since there's nothing else like it in the list of templates, I used it as an abbreviation and copied the same code from "psvm" in.

Now when I type "main" + TAB (without the quotes of course) I get my main method.

It is redundant, but more intuitive for me.

To create "main" go to Tools->Options, click the "Editor" Icon, then the "Code Templates" tab.

  • Make sure that the "Language" combo is set to "Java"
  • Click the "New" button that's to the right of the "Templates" window
  • Enter "main" (without quotes) in the "Abbreviation" textbox that pops up
  • Enter the template code in the "Expanded Text" window below

my entry looks like this:

Abbreviation

main           

Expanded Text

public static void main(String[] args) {$cursor}

Expanded Text (Code Window)

public static void main(String[] args) {
    ${cursor}
}

Of course, you can always have Netbeans create your application's main class with the main method inserted by default.

You do that by Choosing "Java Main Class" from the "New File" dialog instead of "Java Class". That will do it.

Cheers!

Upvotes: 6

Tobias Kremer
Tobias Kremer

Reputation: 1641

If you want to just run some test why not use your testing framework? like JUnit:

@Test
public void test() {
    // do something
}

This way you can even store the test for later usage. It is properbly in most cases not a good idear to think of tests as something to execute once and then throw away.

Upvotes: 2

Lukasz Czerwinski
Lukasz Czerwinski

Reputation: 15432

Thanks to predefined code templates in Netbeans it's simple: just type psvm and press Tab.

psvm is an acronym for: public static void main

Upvotes: 63

Related Questions