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

People,

I used to have only one jar file, and the manifest was set up in such a way that I can just run my program like:

 java -jar MyApp.jar

Now I divided my functionality into two jar files - MyCore.jar and MyApp.jar.

The following command works:

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

But I can't get the following to work

java -cp "*" -jar MyApp.jar

I get a ClassNotFoundException.

I prefer to use the "-jar" switch. Is there any way to make it work?

Thank you in advance for your help.

Regards,
Peter

+5
source share
4 answers

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 

jar, , Class-Path. , , java -jar- -cp.

+4

Java cli -cp -jar

-jar , JAR. - JAR . ,                JAR Main-Class: classname. classname , static void main (String [] args) , . . Jar Jar trail Java Jar Jar-file mani-               . , JAR , . , JAR, "java -jar", , "java -jar". . Java (JAR)               .

, , jar , , .

+10

, , main(), . . : JAR .

. -jar, -cp.

+3

, jar , com.mycompany.mypackage.App - , :

java -cp MyCore.jar;MyApp.jar com.mycompany.mypackage.App

.

If you insist on using the switch -jar(why?), Then you need to modify the manifest file, add the key Class-Pathand specify the path to all related jar files. This makes your application less flexible if the jar files are not packaged with the jar file of the main application.

+3
source

All Articles