23

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?

5
  • 2
    I'd like to know why this was downvoted.
    – nanofarad
    Sep 5, 2013 at 12:38
  • 1
    I didn't downvoted but it could be because the OP ask&answer his own question within one minute
    – Julien
    Sep 5, 2013 at 12:42
  • It looks like a silly sock-puppet question. Let's go through the NetBeans help file and post each bit of information there as a question/answer on stackoverflow. Sep 5, 2013 at 13:05
  • I'd like to know it too... Sep 5, 2013 at 16:59
  • I have typed in the question because I tried to find an answer on Stack Overflow and couldn't. I've found answer on forum, so decided to post a Q&A at once. There is even such a checkbox in "Ask Question" - it is called: "Answer your own question". If you click it, a form for an answer will show up just under your question. So it's pretty normal to post a question and answer at the same time. Sep 5, 2013 at 17:00

3 Answers 3

63

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

0
6

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

1
  • 1
    Update: NetBeans 11.2 has both "psvm" and "main." Dec 17, 2020 at 22:01
2

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.

4
  • 3
    he wants to know how to quickly get a main() method in a class, not doing some tests.. Oct 12, 2015 at 10:15
  • 2
    downvoting a downvote just because it's your answer and without any comment. Pretty useful behaviour. Oct 12, 2015 at 15:42
  • This answer has absolutely nothing to do with the question.
    – Orsinus
    Oct 12, 2018 at 7:13
  • I like this answer. I had the same question as the OP. I wanted to test a class. The 'create a test' was the reason I wanted psvm. This is the better answer for me, and maybe for the OP.
    – downeyt
    Jun 15, 2019 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.