What are Java Path and Classpath?

In Java
August 05, 2023


Java PATH and CLASSPATH are the two most important environment variables of the Java runtime used to locate JDK binary files for compiling and running Java on both Windows and Linux. They also help find the compiled Java bytecode files known as class files.

What is Java PATH and How to Add It?

Java PATH and CLASSPATH are the two most crucial environment variables of the Java runtime. They play a vital role in locating the JDK binary files necessary for compiling and executing Java on both Windows and Linux platforms. Additionally, these environment variables help locate the compiled Java bytecode files, known as class files, which are essential for running Java applications.

The Java PATH variable specifies the directories where the Java executable files (such as ‘javac’ and ‘java’) are located. When you run a Java program, the system uses this PATH variable to find the appropriate Java binary files, enabling seamless execution.

The most common cause of dreaded errors like Java.lang.NoClassDefFoundError and Java.lang.ClassNotFoundException in Java is incorrectly configured or missing CLASSPATH.

To run console-based Java programs on Windows or Linux environments, we need to use the ‘java’ and ‘javac’ commands. Since we haven’t specified the location of these executable files, the operating system cannot recognize the ‘java’ and ‘javac’ commands. Therefore, we need to provide the path to the directory where the executable files are located.

The Java executable and binary files are located within the ‘bin’ directory of the JDK you are using. After setting this directory path, the OS can access all the necessary elements in the program, including the compiler or interpreter itself.

For example, if you haven’t set up the PATH previously, when you run the ‘java -version’ command to find out the installed Java version on your computer, you will encounter the error “‘java’ is not recognized as an internal or external command, operable program, or batch file.

The reason you are encountering the error is that your operating system doesn’t know where the executable Java file is located. To inform your operating system about the location of this file, you need to add a PATH.

To add the bin directory containing JDK executables to the operating system’s PATH, follow these steps:

For Windows:

  1. Locate the JDK installation directory on your computer. It typically has a structure like: C:\Program Files\Java\jdk1.x.x_xx\bin. Note down this directory path.
  2. Right-click on “This PC” (or “My Computer” in older versions of Windows) and select “Properties.”
  3. Click on “Advanced system settings” on the left-hand side of the window.
  4. In the System Properties window, click on the “Environment Variables” button at the bottom.
  5. In the Environment Variables window, under “System variables,” scroll down to find the “Path” variable and select it. Click on the “Edit” button.
  6. Click on the “New” button and paste the JDK bin directory path (e.g., C:\Program Files\Java\jdk1.x.x_xx\bin) at the end of the existing variable value. Separate it from the previous path with a semicolon (;).
  7. Click “OK” to save the changes.
  8. Close all the windows, and now your operating system should recognize the ‘java’ and ‘javac’ commands.

For Linux or macOS:

  1. Open a terminal window.
  2. Locate the JDK installation directory on your computer. It typically has a structure like: /path/to/jdk1.x.x_xx/bin. Note down this directory path.
  3. Open your bash profile or bashrc file using a text editor (e.g., nano, vi, or any other text editor you prefer). For example, in the terminal, you can use: nano ~/.bash_profile or nano ~/.bashrc
  4. Add the following line at the end of the file, replacing “/path/to/jdk1.x.x_xx/bin” with the actual JDK bin directory path:
export PATH="/path/to/jdk1.x.x_xx/bin:$PATH"
  1. Save the file and exit the text editor.
  2. To apply the changes, either restart your terminal or run the command:
codesource ~/.bash_profile


codesource ~/.bashrc

After following these steps, the bin directory with JDK executables should be added to the operating system’s PATH, and you can use ‘java’ and ‘javac’ commands from any directory in the command prompt or terminal.


In conclusion, understanding and properly configuring Java PATH and CLASSPATH variables are crucial aspects of Java development. The Java PATH variable allows the operating system to locate the Java executable files (java and javac) required for compiling and running Java programs. On the other hand, the CLASSPATH variable helps the Java Virtual Machine (JVM) find the necessary compiled Java class files and external libraries during runtime.

By setting up the PATH and CLASSPATH variables correctly, developers can ensure smooth and efficient execution of Java applications across different platforms, such as Windows and Linux. It is essential to keep these environment variables up-to-date with the appropriate JDK installation directories to avoid common errors like Java.lang.NoClassDefFoundError and Java.lang.ClassNotFoundException.

Taking the time to understand the significance of Java PATH and CLASSPATH variables and their proper utilization can significantly enhance the development process and allow for seamless Java programming. Whether for individual projects or enterprise-level applications, a well-configured PATH and CLASSPATH contribute to a more productive and error-free Java development environment. As Java continues to evolve, maintaining good practices for managing these environment variables becomes increasingly valuable in building robust and sustainable Java applications.