Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Eclipse template for auto-generated main method?

When I create a new class in Eclipse Juno and auto-add a main method, I get the following:

public class Example {

  /**
   * @param args
   */
  public static void main(String[] args) {
    // TODO Auto-generated method stub

  }    
}

I would like to edit this method template to add throws Exception.

I tried editing the template at Preferences > Java > Editor > Templates > "main", however this doesn't affect the scenario above. Instead, this configures the code which is inserted when I type "main" and press Ctrl+Space.

Is this possible?

like image 529
Duncan Jones Avatar asked May 22 '13 07:05

Duncan Jones


People also ask

How do I change the main method in eclipse?

One shortcut that really helps me, Eclipse has syntax suggestions, and if you type "main" into the terminal window and hit control + space, you can choose main method and it'll populate it complete with enclosed braces. Helpful if you're not always quick to remember syntax off the top of your head!

How do I add a main method to a class in Eclipse?

To get public static void main(String[] args) line in eclipse without typing the whole line type main and press Ctrl + space then, you will get the option for the main method select it.

How do I import a template into eclipse?

To import a code template: Click Import to open the "Import Templates" browser. Select the relevant XML file containing the template information. Click Open.


1 Answers

The only way I know for creating your own new class / new project template, is by creating your own plugin. This requires some effort. I am not sure if it is worth doing it only for adding a throw to main.

Here is a tutorial for it.

like image 197
Mike de Dood Avatar answered Oct 03 '22 14:10

Mike de Dood