Peter Peter - 17 days ago 5
Java Question

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

Folks,

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.

Regards,

Peter

Answer

I have a Manifest.mf 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.

Comments