1. Overview
1.概述
Classpath is an essential concept in the Java world. When we compile or start a Java application, the JVM finds and loads the classes in the classpath.
Classpath是Java世界中的一个基本概念。当我们编译或启动一个Java应用程序时,JVM会在classpath中找到并加载类。
We can define the elements in the classpath either through the -cp option of the java/javac commands or through the CLASSPATH environment variable. No matter which approach we take to set the classpath, we need to follow the classpath syntax.
我们可以通过java/javac命令的-cp选项或者通过CLASSPATH环境变量来定义classpath中的元素。无论我们采取哪种方法来设置classpath,我们都需要遵循classpath的语法。
In this quick tutorial, we’ll discuss the classpath syntax, and particularly, the classpath separator on Windows and Linux operating systems.
在这个快速教程中,我们将讨论classpath语法,特别是Windows和Linux操作系统中的classpath分离器。
2. The Classpath Separator
2.阶梯式分离器
The classpath syntax is actually pretty straightforward: a list of paths separated by the path separators. However, the path separator itself is system-dependent.
classpath 语法实际上是非常直接的:一个由路径分隔符分隔的路径列表。然而,路径分隔符本身是依赖于系统的。
While the semicolon(;) is used as the separator on Microsoft Windows systems, the colon (:) is used on Unix-like systems:
虽然分号(;)在微软Windows系统中被用作分隔符,但冒号(:)在类Unix系统中被使用:。
# On Windows system:
CLASSPATH="PATH1;PATH2;PATH3"
# On Linux system:
CLASSPATH="PATH1:PATH2:PATH3"
3. The Misleading Man Page on Linux
3.关于Linux的误导性手册页面
We’ve learned that the classpath separator can be different depending on the operating system.
我们已经了解到,根据操作系统的不同,classpath分隔符可能是不同的。
However, if we take a closer look at the Java man page on Linux, it says the classpath separator is the semicolon (;).
然而,如果我们仔细看看Linux上的Javaman页面,它说classpath分隔符是分号(;)。
For example, the man page of the java command from the latest(ver.17) OpenJDK shows:
例如,最新的(ver.17)OpenJDK的man命令的java页显示。
–class-path classpath, -classpath classpath, or -cp classpath
A semicolon (;) separated list of directories, JAR archives, and ZIP archives to search for class files.
…-class-path classpath, -classpath classpath, 或 -cp classpath
一个分号(;)分隔的目录、JAR档案和ZIP档案的列表,用于搜索类文件。
…
Also, we can find the exact text in the Oracle JDK‘s manual.
另外,我们可以在Oracle JDK的手册中找到确切的文本。
This is because Java is currently using the same manual content for different systems. A corresponding bug issue has been created earlier this year.
这是因为Java目前在不同的系统中使用相同的手册内容。相应的bug问题已经在今年早些时候被创建。
Moreover, Java has clearly documented that the path separator is system-dependent on the File class’s pathSeparatorChar field.
此外,Java有明确的文件规定,路径分隔符是依赖于系统的File类的pathSeparatorChar字段。
4. Conclusion
4.总结
In this short article, we’ve discussed the classpath syntax on different operating systems.
在这篇短文中,我们已经讨论了不同操作系统上的classpath语法。
Further, we’ve talked about a bug regarding the path separator in the Java man page on Linux.
此外,我们已经谈到了一个关于Linux上Java手册页中的路径分隔符的错误。
We should keep in mind that the path separator is system-dependent. The colon is used on Unix-like systems, while on Microsoft Windows systems, the semi-colon is used.
我们应该记住,路径分隔符是由系统决定的。在类似Unix的系统中使用冒号,而在Microsoft Windows系统中则使用分号。