java 默认情况下,当前路径`.` 是否在类路径中?

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

Is the current path `.` in the classpath by default?

javaubuntu

提问by Tim

Do the second command add the current path as another path for searching?

第二个命令是否将当前路径添加为另一个搜索路径?

javac -cp /home/tim/program_files/programming/java/junit-4.11.jar MyTest.java

javac -cp .:/home/tim/program_files/programming/java/junit-4.11.jar MyTest.java

Is it the correct way to separate multiple paths, by a colon?

这是用冒号分隔多条路径的正确方法吗?

Isn't the current path always in ClassPath by default, and thus no need to explicitly specify?

默认情况下,当前路径不是总是在 ClassPath 中,因此不需要显式指定吗?

Thanks.

谢谢。

回答by Voicu

From Oracle's pageon setting the class path:

从 Oracle关于设置类路径的页面

The default class path is the current directory. Setting the CLASSPATH variable or using the -classpathcommand-line option overridesthat default, so if you want to include the current directory in the search path, you must include "." in the new settings.

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

Use ;for Windows and :for Unix-like operating systems as a separator for multiple paths.

使用;了Windows和:类Unix操作系统作为多条路径的分隔符。

回答by Amr Lotfy

If you add classpath then current path is omitted, which is a very very unpleasantand unexpectedbehavior :(

如果添加类路径,则省略当前路径,这是一个非常非常不愉快意外的行为:(

Moreover to add current path I found (at least for ubuntu) that IT IS NOT ENOUGHto add .in classpath but you have to add ./*

此外,要添加当前路径,我发现(至少对于 ubuntu)添加类路径还不够.但您必须添加./*

For example (this will notwork)

例如(这不起作用)

java -ea -cp ".:lib/*" org.testng.TestNG suites/regression.xml

java -ea -cp ".:lib/*" org.testng.TestNG suites/regression.xml

will NOTwork if you have a jar file in current path

,如果你有在当前路径中的JAR文件工作

the correct one is

正确的是

java -ea -cp "./*:lib/*" org.testng.TestNG suites/regression.xml

java -ea -cp "./*:lib/*" org.testng.TestNG suites/regression.xml

I hope no one shoots himself or have a heart attack!

我希望没有人开枪自杀或心脏病发作!

回答by Jigar Joshi

Do the second command add the current path as another path for searching?

第二个命令是否将当前路径添加为另一个搜索路径?

Yes

是的

Is it the correct way to separate multiple paths, by a colon?

这是用冒号分隔多条路径的正确方法吗?

depends on platform, in unix platform :works, in windows you need ;

取决于平台,在 unix 平台上:工作,在 windows 中你需要;

Isn't the current path always in ClassPath by default, and thus no need to explicitly specify?

默认情况下,当前路径不是总是在 ClassPath 中,因此不需要显式指定吗?

Current directory is present by default unless you override it with -cpin first case it is not present in second case it is

当前目录默认存在,除非你-cp在第一种情况下覆盖它,在第二种情况下它不存在

回答by rolfl

From the help page (FOR WINDOWS):

帮助页面(对于 WINDOWS)

-classpath classpath
-cp classpath

    Specifies a list of directories, JAR files, and ZIP archives to
    search for class files. Separate class path entries with semicolons
    (;). Specifying -classpath or -cp overrides any setting of the
    CLASSPATH environment variable.

    If -classpath and -cp are not used and CLASSPATH is not set, then the
    user class path consists of the current directory (.).
-classpath classpath
-cp classpath

    Specifies a list of directories, JAR files, and ZIP archives to
    search for class files. Separate class path entries with semicolons
    (;). Specifying -classpath or -cp overrides any setting of the
    CLASSPATH environment variable.

    If -classpath and -cp are not used and CLASSPATH is not set, then the
    user class path consists of the current directory (.).

Note that, on windows, the path separator is a ;semicolon.

请注意,在 Windows 上,路径分隔符是;分号。

On other platforms the separator is the colon :.

在其他平台上,分隔符是冒号:

This conforms with the standard path-like systems on the various platforms.

这符合各种平台上的标准路径类系统。