What is a CLASSPATH in terms of Java? The CLASSPATH variable is one way to tell applications, including the JDK tools, where to look for user classes. The CLASSPATH tells the JDK tools and applications where to find third-party and user-defined classes that are not extensions or part of the Java platform. The CLASSPATH needs to find any classes you have compiled with the javac compiler.
Let’s begin by writing the infamous HelloWorld Java program, compile it, and then run it to demonstrate our lack of a proper development environment.
public class HelloWorld
public static void main(String args)
System.out.println("Java Code Depot Says Hello You Crazy World!!!");
When trying to compile HelloWorld.java. we get an error message that javac is not recognized. This means we need to also edit the PATH statement in the environmental variables.
Let’s check to see if our CLASSPATH has already been set using the echo %CLASSPATH% command line statement. When running this command in a CMD window, it returns blank which means CLASSPATH is not set.
Since I am running on a Windows platform (10), I will need to set CLASSPATH in the windows Environmental Variables window.
Open your Control Panel
Click on System and Security
Click on System
Click on Advanced System Settings
Click on Environmental Variables
Let’s edit the PATH to include the path to our JDK 10 bin folder and then click OK.
Close the CMD window and then open again. Our path now includes the recent JDK 10 bin install entry and we can compile HelloWorld.java successfully.
Now if we run our HelloWorld program from anywhere other than where the HelloWorld.class file is located, we get ClassNotFoundException message.
NOW we can set the CLASSPATH to our Java Development folder, which for this lesson is in C:\Users\Admin\JavaDev folder. Let’s add a new System Variable called CLASSPATH and set it to C:\Users\Admin\JavaDev folder. Click OK, OK, and OK on all open environmental windows.
SUCCESS! We can now run our HelloWorld Java executable from any folder and because we set the CLASSPATH to our Java development folder, the program runs as it should. You can also set the CLASSSPATH on the command line using the “-cp” parameter followed by the location of your class files.
This concludes lesson 2 on setting the CLASSPATH for the Java environment on your laptop or computer. Your now ready to move onto the next lesson.