Change Eclipse template for auto-generated main method?
Asked Answered
L

2

6

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?

Lehrer answered 22/5, 2013 at 7:45 Comment(0)
C
3

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.

Cimbri answered 22/5, 2013 at 8:13 Comment(2)
Hey @Vishnu, try out the below answer. It might help.Brigade
@Vishnu I've edited the link with the Internet-Wayback-Machine, so its working again.Dari
B
1

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 {

    }
Brigade answered 8/7, 2018 at 19:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.