简体   繁体   English

Java类路径和相对路径

[英]Java Classpath and relative paths

If you have a relative path in the java class path, does this just search the current working directory. 如果在java类路径中有相对路径,那么这只是搜索当前工作目录。 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? 在清单文件中它是相对于jar所在的目录?

Ex. 防爆。 Cmdline CMDLINE

java -cp somejar.jar

Or 要么

Manifest 表现

Class-Path: somejar.jar

If you say -cp somejar.jar you are adding somejar.jar to the classpath. 如果你说-cp somejar.jar你将somejar.jar添加到类路径中。 It will only try to find the somejar.jar from the directory you are currently in when you typed the command. 它只会尝试在您键入命令时从当前所在的目录中找到somejar.jar

If you say in the Manifest 如果你在Manifest中说

Class-Path: somejar.jar

you are saying add the jar, somejar.jar into the classpath from the directory where the manifest is located(aka inside the jar). 你是说将jar, somejar.jar添加到清单所在目录的类路径中(也就是在jar中)。

As a side note, when you specify the classpath by using -jar, -cp, or -classpath, you override the system variable CLASSPATH. 另请注意,使用-jar,-cp或-classpath指定类路径时,将覆盖系统变量CLASSPATH。

More details can be found here, http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html 更多细节可以在这里找到, http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

You've actually mentioned two different cases: 你实际上提到过两种不同的情况:

Case #1 情况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. 在这种情况下,相对于当前目录解析JAR(或其他)的相对路径。

Case #2 案例#2

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

where somejar.jar contains 其中somejar.jar包含

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". 在这种情况下,相对于当前目录(如上所述)解析“foo / bar / somejar.jar”,但相对于包含“somejar.jar”的目录解析“anotherjar.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.) (旁白:我的理解是当通过-cp$CLASSPATH包含JAR文件时,会遵守Manifest Class-Path:属性,但它只会影响从该JAR加载的类的依赖关系...请参阅“查找类”链接下面。)

References: 参考文献:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM