Java 类路径和相对路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16502663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Classpath and relative paths
提问by rubixibuc
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?
如果在 java 类路径中有相对路径,是否只搜索当前工作目录。对于清单文件中声明的类路径是否同样适用。在清单文件中,它是否相对于 jar 所在的目录?
Ex. Cmdline
前任。命令行
java -cp somejar.jar
Or
或者
Manifest
显现
Class-Path: somejar.jar
回答by greedybuddha
If you say -cp somejar.jar
you are adding somejar.jar
to the classpath. It will only try to find the somejar.jar
from the directory you are currently in when you typed the command.
如果您说-cp somejar.jar
要添加somejar.jar
到类路径。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).
您是说将 jarsomejar.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
回答by Stephen C
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”是相对于当前目录(如上)解析的,但“anotherjar.jar”是相对于包含“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.)
(另外:我的理解是,当通过-cp
或包含 JAR 文件时$CLASSPATH
,会遵循Manifest Class-Path: 属性,但它只会影响从该 JAR 加载的类的依赖关系……请参阅下面的“findingclasses”链接。)
References:
参考: