What is Java CLASSPATH?3 min read

Classpath is a fundamental concept that has been oblivious to many of us, especially when working with modern IDEs like IntelliJ IDEA; we don’t have to worry much about classpaths or other detail to make a program runs. However, it is essential for any Java developer to know what the classpath is and why it exists to understand how the JVM finds dependencies of our classes and how our classes can be found.

In simple terms, when executing a program, the classpath will help Java to find locations where our classes exist. Remember, a list of .class files will be generated when compiling the program. The classpath needs to specify the locations of these class files so the JVM can look up at them to find our classes (since it cannot look at every single file of our computer) to execute the program. If you’re familiar with environment variables in the OS, you can think of the PATH variables that determine the locations of executables, while classpath variables decide the locations of Java classes and packages.

Suppose we have a class named TestedClass.java existing in the com.learntocodetogether.example.classpath package like this:

package com.learntocodetogether.example.classpath;

public class TestedClass {}

We will compile our program and place the compiled TestedClass.class file in the build folder. And since each layer of packages will be a folder, our TestedClass.class will reside at build/com/learntocodetogether/example/classpath/TestedClass.class and other files in that package (if we have any). Now, in order to JVM can find the TestedClass.class file, our classpath needs to contain the build directory. Next, we want to introduce a dependency to our TestedClass, another class locates on a different package:

package com.learntocodetogether.example.classpath;
import test.example.glitter.Shine;

public class TestedClass {}

Presume that the Shine.java class will also be compiled into the build folder; the JVM can look up build/test/example/glitter/Shine.class to find out our dependency in the TestedClass.

Also, you can specify the jar file in the CLASSPATH. For example, we use some build tools for packaging our Shine class (which is a dependency in the TestedClass) into the lib folder and name it dependency.jar and remove this class from our directory. In our source code for TestedClass, if we’re using some IDE such as IntelliJ IDEA it may render some errors that tell us the file doesn’t exist or we need to add jar files to the CLASSPATH.

In our class, we need to add dependency.jar into our CLASSPATH, and the JVM will look into this jar file to determine the location of the Shine class, which our TestedClass depends on.

How to set the CLASSPATH variables?

There are several options that we can go with to set up the classpath variables, and the most adopted one is the java -cp <path of your class>, and we can separate our classpath using the : operator (in Unix-like OS), the last argument specifies our main class:

java -cp "/path/to/our/class:/another/path/to/our/class:/glitter/path/to/class" TestedClass

We can also set CLASSPATH variables as our environment variables, but this approach is less recommended (also in Unix-like machine):

export CLASSPATH="/path/to/our/class:/another/path/to/our/class:/glitter/path/to/class"

Another approach is to let the IDE do this for you; running our program in some modern IDEs, such as IntelliJ IDEA automatically sets up all necessary class paths for us if needed.

Previous Article
Next Article
Every support is much appreciated ❤️

Buy Me a Coffee