I'm having problems with running multiple different classes from one JAR file. I know that I can set one of the classes inside JAR to by Main class that will be run after command java -jar myjar.jar
, but what I want is something like:
java -jar myjar.jar MyClass
Is it possible to do this that way, or do I have to create multiple JAR (each for one runnable class), or is it better to create 'manager' class that will run my other classes passing to them command line arguments?
I was looking for documentation or reference, but I couldn't find any.
Jar files can contain only one Main-Class attribute in the manifest, which means a jar can contain only one mainClassName.
The executable Jar file format only allows you to specify one main class. In order for you to be able to execute different applications, you'll need to either create a "manager" as you suggest, or to use the classpath instead:
java -cp myjar.jar MyClass
However, this approach will ignore the classpath you have configured in the Jar's manifest file.
you will have to use:
java -cp myjar.jar MyClass
and
java -cp myjar.jar OtherMainClass
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With