-classpath
选项是否会添加或替换CLASSPATH
环境变量的内容?
-classpath
选项是否会添加或替换CLASSPATH
环境变量的内容?
使用classpath变量可以覆盖环境变量中的CLASSPATH,但仅在该会话期间生效。如果重新启动应用程序,则需要再次设置classpath变量。
public class CPTest {
public static void main (final String[] args) {
String cp = System.getenv("CLASSPATH");
System.out.println(cp);
}
}
%echo $CLASSPATH
/home/test/:.
%java CPTest
/home/test/:.
%java -cp /home/xanadu:. CPTest
/home/test/:.
%java -cp /home/test CPTest
Exception in thread "main" java.lang.NoClassDefFoundError: CPTest
是的。引用自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 (.).
只能使用其中一个选项,不能同时使用。
指定-classpath或-cp会覆盖CLASSPATH环境变量的任何设置。
...
-classpath选项更为推荐,因为您可以为每个应用程序单独设置它,而不影响其他应用程序,并且其他应用程序也不会修改其值。
...
设置CLASSPATH变量或使用-classpath命令行选项会覆盖默认设置,因此如果您想将当前目录包含在搜索路径中,则必须在新设置中包括“.”。
java -cp tests Test.java
时,似乎它仍然从“.”(当前工作目录)中发现类。 - markasoftware