Selecting main class in a runnable jar at runtime

Tomasz picture Tomasz · Jan 7, 2010 · Viewed 47.7k times · Source

I have two main classes in the app. When I package it to a runnable jar (using Eclipse export function) I have to select a default main class.

Is there a way to access the non-default main class from the jar at runtime?

Answer

Jeremy Raymond picture Jeremy Raymond · Jan 7, 2010

You can access both via java -cp myapp.jar com.example.Main1 and java -cp myapp.jar com.example.Main2. The default main class in the jar is for when you invoke your app via java -jar myapp.jar.

See JAR_(file_format) for more details. When you select the main class in Eclipse this is what gets set in: Main-Class: myPrograms.MyClass inside of the jar manifest META-INF/MANIFEST.MF in side of the jar file.