Zac*_*112 19 java jvm classpath environment-variables jvm-arguments
是否可以使用-classpath
选项java
,添加或替换CLASSPATH
env变量的内容?
是.引自java(1)
手册页:
-classpath classpath
-cp classpath
Specifies a list of directories, JAR archives, and ZIP archives to search for class files. Class
path entries are separated by colons (:). Specifying -classpath or -cp overrides any setting of the
CLASSPATH environment variable.
If -classpath and -cp are not used and CLASSPATH is not set, the user class path consists of the cur-
rent directory (.).
Run Code Online (Sandbox Code Playgroud)
使用其中一个选项,而不是两个选项.
指定-classpath或-cp会覆盖CLASSPATH环境变量的任何设置.
...
-classpath选项是首选,因为您可以为每个应用程序单独设置它,而不会影响其他应用程序,也不会修改其值的其他应用程序.
...
设置CLASSPATH变量或使用-classpath命令行选项会覆盖该缺省值,因此如果要在搜索路径中包含当前目录,则必须包含".".在新设置中.
-cp选项的使用不会影响CLASSPATH环境变量.
您可以尝试使用此小代码片段来检查:
public class CPTest {
public static void main (final String[] args) {
String cp = System.getenv("CLASSPATH");
System.out.println(cp);
}
}
Run Code Online (Sandbox Code Playgroud)
%echo $CLASSPATH
/home/test/:.
Run Code Online (Sandbox Code Playgroud)
没有-cp选项的输出:
%java CPTest
/home/test/:.
Run Code Online (Sandbox Code Playgroud)
带-cp选项的输出:
%java -cp /home/xanadu:. CPTest
/home/test/:.
Run Code Online (Sandbox Code Playgroud)
两个调用的输出相同(一个用-cp,另一个没用).
也可以使用CLASSPATH环境变量中指定的路径,也可以
使用使用-cp选项指定的路径.它不是两者的混合,而是其中之一.
从下面的调用中可以看出这一点.如果
从-cp选项中排除CWD(当前工作目录".")
,则JVM启动程序(即java)无法找到
类文件,尽管其中包含CWD(".")的CLASSPATH环境变量.
%java -cp /home/test CPTest
Exception in thread "main" java.lang.NoClassDefFoundError: CPTest
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
50223 次 |
最近记录: |