![]() |
VOOZH | about |
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.
GeekForGeeks!
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\librariesNote: 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\libs1. Select Start
2. Go to the Control Panel
👁 Select Control Panel3. Select System and Security
👁 select Advanced System Settings4. Select Advanced System settings
👁 Image5. Click on Environment Variables
👁 Click on Environment variables6. Click on New under System Variables
👁 Click on New under System Variables7. Add CLASSPATH as variable name and path of files as a variable value.
👁 Add CLASSPATH as variable name and path of files as a variable value8. Select OK.
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
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.