天天看點

java -jar 主類,在運作時選擇可運作jar中的主類

java -jar 主類,在運作時選擇可運作jar中的主類

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?

解決方案

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.