Search code examples
javaeclipsejakarta-eeprogram-entry-point

Java - Eclipse - How to force the application to run only one main?


I have a client application (project) in Eclipse JavaEE. Whenever I choose to run the project, multiple mains will appear.

My question is: Is there any method that we can force the project to call only one specific main whenever I run the project?


Solution

  • If you are running the application from within Eclipse itself, then you will need to setup a run configuration pointing to the main class you want.

    The documentation for creating a run configuration can be found here:

    http://help.eclipse.org/oxygen/topic/org.eclipse.jdt.doc.user/tasks/tasks-java-local-configuration.htm?cp=1_3_6_3

    "The Main tab defines the class to be launched. Enter the name of the project containing the class to launch in the project field, and the fully qualified name of the main class in the Main class field. Check the Stop in main checkbox if you want the program to stop in the main method whenever the program is launched in debug mode. Note: You do not have to specify a project, but doing so allows a default classpath, source lookup path, and JRE to be chosen."

    If you are exporting your application to a .jar file, then you will want to specify the main class inside the manifest file. Instructions to do that can be found here:

    http://help.eclipse.org/oxygen/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-35.htm

    The main class entry in the manifest file should look like:

    Main-Class: classname
    

    For example:

    Main-Class: somepackage.SomeClass