Java Class Path and Relative Paths

If you have a relative path in the java class path, is it only looking for the current working directory. The same is true for class paths declared in the manifest file. In the manifest file, does this refer to the directory where the jar is located?

Ex. Command line

java -cp somejar.jar 

or

manifest

 Class-Path: somejar.jar 
+4
source share
2 answers

If you say -cp somejar.jar , you add somejar.jar to the classpath. He will try to find somejar.jar from the directory you are currently in when you typed the command.

If you say in the manifest

 Class-Path: somejar.jar 

you say, add jar, somejar.jar to the class path from the directory in which the manifest is located (it is inside the jar).

As a side note, when you specify a class path using -jar, -cp, or -classpath, you override the CLASSPATH system variable.

More information can be found here http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

+6
source

In fact, you mentioned two different cases:

Case No. 1

 java -cp foo/bar/somejar.jar ... 

In this case, the relative path to the JAR (or any other) is allowed relative to the current directory.

Case No. 2

 java -jar foo/bar/somejar.jar ... 

where somejar.jar contains

 Class-Path: anotherjar.jar 

In this case, "foo / bar / somejar.jar" is resolved relative to the current directory (as indicated above), but "anotherjar.jar" is resolved relative to the directory containing "somejar.jar".

(Also: I understand that the Manifest Class-Path: attribute is recognized when a JAR file is included via -cp or $CLASSPATH , but it only affects the dependencies of classes loaded from this JAR ... see the findclasses link below. )

References:

+3
source

Source: https://habr.com/ru/post/1480294/


All Articles