Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use manifest.mf class-path for alternative main in same jar?

I have created an executable jar file that contains two main classes. All libraries are included in the jar and the main Main-Class works fine when executing like this:

java -jar MyApplication.jar

But when I try to run the other main class like this:

java -cp MyApplication.jar my.other.mainClass

It does not include the classpath of the manifest.mf and it can not find the libraries that are in the jar file.

Is there a simple way so that the other main class can use the classpath from the manifest.mf? or should I create two separate executable jars?

like image 462
Blem Avatar asked Jan 18 '26 01:01

Blem


2 Answers

You could write a class that invokes the main method of whatever class is passed as its first argument using Reflection - and configure this as the Main-class in your jar. This way you can invoke multiple main methods from the same jar with java -jar file.jar my.other.mainClass

like image 94
kjp Avatar answered Jan 20 '26 13:01

kjp


Is there a simple way so that the other main class can use the classpath from the manifest.mf? or should I create two separate executable jars?

The JAR manifest classpath is only used if you use -jar option, and conversely the command line argument is only interpreted as a classname if -jar is NOT used. You cannot mix the two approaches.

However, this doesn't mean you have to create a second JAR file. For instance, you could write a simple shell script to launch the JVM using the classpath copied from the manifest and the secondary entry point classname.

like image 32
Stephen C Avatar answered Jan 20 '26 14:01

Stephen C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!