Peter Peter - 1 year ago 59
Java Question

Java using "-cp" and "-jar" together


Earlier, I had just one jar file and the manifest was set up such that I can simply run my program as:

java -jar MyApp.jar

Now, I have separated my functionality into two jar files - MyCore.jar and MyApp.jar.

The following command works:

java -cp "*" com.mycompany.mypackage.App

But I cannot get the following to work

java -cp "*" -jar MyApp.jar

I get a ClassNotFoundException.

I prefer using "-jar" switch. Is there a way to make it work?

Thank you in advance for your help.



Answer Source

I have a file like this.

Manifest-Version: 1.0
Main-Class: com.mycompany.mypackage.App
Class-Path: MyApp.jar MyCore.jar log4j.jar 

You can just add any jar files you need to the Class-Path line. Then as long as the jars are in the class path you can run the java -jar command without -cp.