Duncan Jones
Duncan Jones

Reputation: 69339

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?

Upvotes: 6

Views: 1371

Answers (2)

Mike de Dood
Mike de Dood

Reputation: 393

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.

Upvotes: 3

dixitk13
dixitk13

Reputation: 523

I know this is too late for an answer. Hopefully, someone else finds it useful.

I was trying to do this exact same thing. I wanted to do something like

public class Example {

    public static void main(String[] args) {
        Example sol = new Example();

    }    
}

What I ended up doing is, configuring a code template which auto-generates the above for me.

Edit template at Preferences > Java > Code Style > Code Templates > Code > Class Body > Edit

Adding the following template pattern, lets me generate what I needed

    public static void main(String[] args) {
        ${type_name} sol = new ${type_name}();
    }

For your use case, writing the below template should do it.

    public static void main(String[] args) throws Exception {

    }

Upvotes: 1

Related Questions