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
import java.io.*;
class GFG {
public static void main(String[] args)
{
System.out.println( "GeekForGeeks!" );
}
}
|
Set the CLASSPATH in JAVA in Windows
Command Prompt:
set PATH=.;C:\Program Files\Java\JDK1.6.20\bin
Note: Semi-colon (;) is used as a separator and dot (.) is the default value of CLASSPATH in the above command.
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,
JAVA_HOME = "/usr/lib/jvm/<java folder (eg. java-1.8.0-openjdk-amd64>)/bin"
export JAVA_HOME
CLASSPATH=".:/usr/lib/jvm/<java folder>/lib:/home/name/Desktop"
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}
Last Updated :
20 Nov, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...