How to Set Classpath in Java?
Last Updated :
25 Sep, 2024
How to Set Classpath in Java?
CLASSPATH describes the location where all the required files are available which are used in the application. Java Compiler and JVM (Java Virtual Machine) use CLASSPATH to locate the required files. If the CLASSPATH is not set, Java Compiler will not be able to find the required files and hence will throw the following error.
Error: Could not find or load main class <class name> (e.g. GFG)
The above error is resolved when CLASSPATH is set.
Java
// If the following code is run when the CLASSPATH is not
// set, it will throw the above error.
// If it is set, we get the desired result
import java.io.*;
class GFG {
public static void main(String[] args)
{
// prints GeeksForGeeks to the console
System.out.println("GeekForGeeks!");
}
}
Set the CLASSPATH in JAVA in Windows
Command Prompt:
Open the Command Prompt.
Use the following command to set the CLASSPATH:
set CLASSPATH=.;C:\path\to\your\classes;C:\path\to\your\libraries
Note: The dot (.) represents the current directory, and the semicolon (;) is used as a separator between different paths.
Example:
set CLASSPATH=.;C:\Users\GFG\JavaClasses;C:\Program Files\Java\libs
GUI:
1. Select Start
2. Go to the Control Panel

3. Select System and Security

4. Select Advanced System settings

5. Click on Environment Variables

6. Click on New under System Variables

7. Add CLASSPATH as variable name and path of files as a variable value.

8. Select OK.
Set the CLASSPATH on Linux
Command Line:
Find out where you have installed Java, basically, it’s in /usr/lib/jvm path. Set the CLASSPATH in /etc/environment usingÂ
sudo <editor name> /etc/environment
Add the following lines,
CLASSPATH=".:/path/to/your/classes:/path/to/your/libraries"
export CLASSPATH
Note: Colon (:) is used as a separate directory and dot (.) is the default value of CLASSPATH in the above command.
To check the current CLASSPATH, runÂ
echo ${CLASSPATH}
Difference between PATH and CLASSPATH
- The PATH variable is used to specify the location of the Java binaries (e.g., java, javac). It allows the system to locate the Java executables to compile and run programs.
- The CLASSPATH variable is used to specify the location of the Java class files and libraries required for the program. It tells the JVM and compiler where to look for user-defined classes and packages.
Conclusion
Properly setting the CLASSPATH is crucial for the Java compiler and JVM to locate the required class files and libraries necessary for compiling and running your Java applications. Understanding the difference between the PATH and CLASSPATH is essential, as each serves a distinct purpose. While PATH points to the location of Java executables like javac and java, CLASSPATH is used to specify the locations where the class files or libraries are stored. A correctly configured CLASSPATH ensures that your Java programs run smoothly without errors related to missing class files.
Similar Reads
CLASSPATH in Java
Package in Java is a mechanism to encapsulate a group of classes, sub-packages, and interfaces. Packages are used for: Preventing naming conflicts. For example, there can be two classes with the name Employee in two packages, college.staff.cse.Employee and college.staff.ee.EmployeeMaking searching/l
3 min read
How to Add JAR file to Classpath in Java?
JAR is an abbreviation of JAVA Archive. It is used for aggregating multiple files into a single one, and it is present in a ZIP format. It can also be used as an archiving tool but the main intention to use this file for development is that the Java applets and their components(.class files) can be
4 min read
How to Set Multiple Classpath in Java in Windows?
In JAVA, you have to set PATH and CLASSPATH variables correctly before running any java program. After installing JDK, the next step is to set the path and CLASSPATH variables in the Windows system environment variable. CLASSPATH is an environment variable which is used by Application ClassLoader to
2 min read
Different Ways to Set a Classpath in Java
Java Virtual Machine(JVM) uses classpath to locate the class files to execute the code. If your classpath is not set and the class file is not present in the same directory as your java file, then JVM will be unable to find the required class file, and it will throw an error (java.lang.ClassNotFound
3 min read
How to create a Class in JShell of Java 9
JShell is an interactive Java Shell tool, it allows us to execute Java code from the shell and shows output immediately. JShell is a REPL (Read Evaluate Print Loop) tool and runs from the command line. Jshell have the facility to create a class by which all the efforts can be reduced to write a whol
2 min read
How to Create Custom Class in Java?
Class is the collection of objects. Class is not a real-world entity it is just only templates and prototypes or blueprints. Class does not occupy memory. We can write a custom class as per our choice for an illustration purpose a sample is shown in the program below as a helper class. Example: Java
2 min read
Static class in Java
Java allows a class to be defined within another class. These are called Nested Classes. Classes can be static which most developers are aware of, henceforth some classes can be made static in Java. Java supports Static Instance Variables, Static Methods, Static Block, and Static Classes. The class
3 min read
How to Set Destination of the Class File in Java?
In this article, Let's learn How to Set Destination of the Class File in Java. Before that, let's understand what is a class file. When we compile a java file, we get a class file. A Java class file is a file containing Java byte-code and having " .class" extension that can be executed by JVM. The j
1 min read
How to Import Custom Class in Java?
Java language is one of the most popular languages among all programming languages. There are several advantages of using the java programming language, whether for security purposes or building large distribution projects. One of the advantages of using Java is that it tries to connect every concep
3 min read
How to Execute a .class File in Java?
A Java Class file is a compiled java file. It is compiled by the Java compiler into bytecode to be executed by the Java Virtual Machine. Step #1: Compile the .java File Open Terminal (Mac) or Command Prompt (Windows). Navigate to the folder containing the java file and type the following command to
1 min read