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?
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!
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.
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
"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.
my entry looks like this:
main
public static void main(String[] args) {$cursor}
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With