Java -classpath 选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2161468/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:15:26  来源:igfitidea点击:

Java -classpath option

javajvmclasspathenvironment-variablesjvm-arguments

提问by Zacky112

Will the use of -classpathoption with java, add to or replace the contents of the CLASSPATHenv variable?

将使用-classpath选项 with java,添加或替换CLASSPATHenv 变量的内容吗?

采纳答案by giri

Using the classpath variable it overrides the CLASSPATH of Environment variable but only for that session. If you restart the application you need to again set the classpath variable.

使用类路径变量它会覆盖环境变量的 CLASSPATH,但仅适用于该会话。如果您重新启动应用程序,您需要再次设置类路径变量。

回答by Robert Munteanu

Yes. Quoted from the java(1)man page:

是的。引用自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 (.).

回答by miku

Either one of the options is used, not both.

使用其中一个选项,而不是同时使用两个选项。

Specifying -classpath or -cp overrides any setting of the CLASSPATH environment variable.

指定 -classpath 或 -cp 会覆盖 CLASSPATH 环境变量的任何设置。

...

...

The -classpath option is preferred because you can set it individually for each application without affecting other applications and without other applications modifying its value.

-classpath 选项是首选,因为您可以为每个应用程序单独设置它,而不会影响其他应用程序,也无需其他应用程序修改其值。

...

...

Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.

设置 CLASSPATH 变量或使用 -classpath 命令行选项会覆盖该默认值,因此如果要在搜索路径中包含当前目录,则必须包含“.”。在新设置中。

回答by sateesh

The usage of -cp option will not affect the CLASSPATH environment variable.

-cp 选项的使用不会影响 CLASSPATH 环境变量。

You can try this small code snippet to check this:

你可以试试这个小代码片段来检查这个:

public class CPTest {
    public static void main (final String[] args) {
        String cp = System.getenv("CLASSPATH");
        System.out.println(cp);
    }
}
%echo $CLASSPATH  
/home/test/:.

The output without -cp option:

没有 -cp 选项的输出:

%java CPTest  
/home/test/:.

The output with -cp option:

带有 -cp 选项的输出:

%java -cp /home/xanadu:. CPTest  
/home/test/:.

The output is same for both invocations (one with -cp and one without).

两次调用的输出相同(一次使用 -cp,另一次不使用)。

Also either the path specified in the CLASSPATHenvironment variable is
used or the path specified with -cpoption is used. It is not a mix of both it is one of them.

此外,要么使用CLASSPATH环境变量中指定的路径,要么
使用-cp选项指定的路径。它不是两者的混合,而是其中之一。

This is evident from the below invocation. If the CWD (current working directory ".")
is excluded from -cp option, the JVM launcher (i.e. java) cannot find the
class file despite the CLASSPATH environment variable containing CWD (".") in it.

从下面的调用中可以明显看出这一点。如果 CWD(当前工作目录“.”
从 -cp 选项中排除,则 JVM 启动器(即 java)无法找到
类文件,尽管 CLASSPATH 环境变量中包含 CWD(“.”)。

%java -cp /home/test CPTest
Exception in thread "main" java.lang.NoClassDefFoundError: CPTest