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.
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.
In this quick tutorial, we'll discuss the classpath syntax, and particularly, the classpath separator on Windows and Linux operating systems.
2. The Classpath Separator
The classpath syntax is actually pretty straightforward: a list of paths separated by the path separators. However, the path separator itself is system-dependent.
While the semicolon(;) is used as the separator on Microsoft Windows systems, the colon (:) is used on Unix-like systems:
# On Windows system: CLASSPATH="PATH1;PATH2;PATH3" # On Linux system: CLASSPATH="PATH1:PATH2:PATH3"
3. The Misleading Man Page On Linux
We've learned that the classpath separator can be different depending on the operating system.
However, if we take a closer look at the Java man page on Linux, it says the classpath separator is the semicolon (;).
For example, the man page of the java command from the latest(ver.17) OpenJDK shows:
–class-path classpath, -classpath classpath, or -cp classpath
A semicolon (;) separated list of directories, JAR archives, and ZIP archives to search for class files.
Also, we can find the exact text in the Oracle JDK‘s manual.
This is because Java is currently using the same manual content for different systems. A corresponding bug issue has been created earlier this year.
Moreover, Java has clearly documented that the path separator is system-dependent on the File class's pathSeparatorChar field.
In this short article, we've discussed the classpath syntax on different operating systems.
Further, we've talked about a bug regarding the path separator in the Java man page on Linux.
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.