Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does java -jar option alter classpath options

Tags:

java

classpath

I have a jar file which mentions the main class in the manifest. When I try to execute the jar using the following command

java -cp .;./* com.foo.MainClass

The code executes and works.

When I try to execute the jar using the following command

java -cp .;./* -jar myjar.jar

I get class not found execptions for some jars which are in the same folder as myjar.jar. I hoping that the -cp option will include those jars in class path. I modified my code to print java.class.path property. In the first case it listed all jars in the current directory, in second case it just listed myjar.jar

I also modified the manifest to add Class-Path element to it with all jars. Then the second command works. But in my code I am trying to load a aribtrary class whose name is provided at command prompt, so I want the class path to contain all jars in a folder. How do I make the second command work in this scenario?

like image 895
mihirg Avatar asked Dec 06 '11 06:12

mihirg


Video Answer


1 Answers

From this,

An executable JAR must reference all the other dependent JARs it requires through the Class-Path header of the manifest file. The environment variable CLASSPATH and any class path specified on the command line is ignored by the JVM if the -jar option is used.

like image 164
Raghuram Avatar answered Oct 09 '22 08:10

Raghuram