Set CLASSPATH

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.

package com.jcd.java.tutorials;

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.

Lesson 2 - Set CLASSPATH

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.

Lesson 2 - Set CLASSPATH

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

Lesson 2 - Set CLASSPATH

Click on System and Security

Lesson 2 - Set CLASSPATH

Click on System

Lesson 2 - Set CLASSPATH

Click on Advanced System Settings

Lesson 2 - Set CLASSPATH

Click on Environmental Variables

Lesson 2 - Set CLASSPATH

Let’s edit the PATH to include the path to our JDK 10 bin folder and then click OK.

Lesson 2 - Set CLASSPATH

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.

Lesson 2 - Set CLASSPATH

Now if we run our HelloWorld program from anywhere other than where the HelloWorld.class file is located, we get ClassNotFoundException message.

Lesson 2 - Set CLASSPATH

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.

Lesson 2 - Set CLASSPATH

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.

Lesson 2 - Set CLASSPATH

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.



Add a Comment

Your email address will not be published. Required fields are marked *