Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert main method in Netbeans (shortcut)

Tags:

java

netbeans

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?

like image 691
Lukasz Czerwinski Avatar asked Sep 05 '13 12:09

Lukasz Czerwinski


People also ask

How do I select a main method in NetBeans?

Running the Application Make sure to save the Java source file, right-click the project and choose Run or choose Run Project under the Run menu. Click Select Main Class. In the Output window (which can be opened from the Window menu), you should see the below. Congratulations!

How do I create a shortcut key in NetBeans?

Editing and Refactoring Code in NetBeans To add a keyboard shortcut for a command: Choose Tools | Options and click the Keymap panel. In the Actions panel, navigate to a command that you want to change, and click Add. In the Add Shortcut dialog box, type in the key combination that you want to use and click OK.


3 Answers

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

like image 151
Lukasz Czerwinski Avatar answered Sep 30 '22 09:09

Lukasz Czerwinski


"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!

like image 6
exe2bin Avatar answered Sep 30 '22 07:09

exe2bin


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.

like image 2
Tobias Kremer Avatar answered Sep 30 '22 07:09

Tobias Kremer