If you have a relative path in the java class path, does this just search the current working directory. Does the same hold for class paths declared in a manifest file. In a manifest file is it relative to the dir the jar is in?
Ex. Cmdline
java -cp somejar.jar
Or
Manifest
Class-Path: somejar.jar
A relative path is a path which doesn't start with the root element of the file system. It is simply the path needed in order to locate the file from within the current directory of your program. It is not complete and needs to be combined with the current directory path in order to reach the requested file.
Classpath is a parameter in the Java Virtual Machine or the Java compiler that specifies the location of user-defined classes and packages. The parameter may be set either on the command-line, or through an environment variable.
Build path is used by the compiler to resolve dependencies and build a project. Classpath is used during runtime when running a project in order to find necessary dependencies. Build path is configured on the Java Build Path property page of a project.
You've actually mentioned two different cases:
Case #1
java -cp foo/bar/somejar.jar ...
In this case the relative path to the JAR (or whatever) is resolved relative to the current directory.
Case #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 above), but "anotherjar.jar" is resolved relative to the directory containing "somejar.jar".
(Aside: my understanding is that a Manifest Class-Path: attribute is respected when a JAR file is included via -cp
or $CLASSPATH
, but it only affects the dependencies of classes loaded from that JAR ... see the "findingclasses" link below.)
References:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With