Skip to content
Related Articles

Related Articles

How to Set Classpath in Java?

View Discussion
Improve Article
Save Article
  • Last Updated : 20 Nov, 2020
View Discussion
Improve Article
Save Article

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!");
    }
}

Output

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

Select Control Panel

3. Select System and Security

select Advanced System Settings

4. Select Advanced System settings

5. Click on Environment Variables

Click on Environment variables

6. Click on New under System Variables

Click on New under System Variables

7. 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 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}

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!