How to set CLASSPATH in Java
The CLASSPATH is a environment variable used by the Application ClassLoader in Java. It plays a crucial role in locating and loading .class files that are not part of the Java platform or its extensions. The CLASSPATH essentially defines the paths where third-party and user-defined classes, as well as JAR files, can be found.
To properly set the CLASSPATH, you need to include all the directories that contain the .class files and JAR files you require. This ensures that Java can locate and load these classes during runtime. By specifying the CLASSPATH, you enable the Java runtime environment to access the necessary classes and libraries needed by your application.
It's important to note that the CLASSPATH is an environment-specific variable and has different syntax for different operating systems. For example, in Windows, you can use the set command to set the CLASSPATH, while in Unix/Linux/macOS, you can use the export command. You should separate multiple paths using a semicolon (;) on Windows and a colon (:) on Unix/Linux/macOS.
1. Search View advance setting in Windows.
![How to set CLASSPATH in Java](https://static.tutorialandexample.com/java/how-to-set-classpath-in-java1.png)
2. Click on Advanced and then on environment variables.
![How to set CLASSPATH in Java](https://static.tutorialandexample.com/java/how-to-set-classpath-in-java2.png)
3. Go to System Variables and Click on new
![How to set CLASSPATH in Java](https://static.tutorialandexample.com/java/how-to-set-classpath-in-java3.png)
4. Then click on Java Jar File after this click on the bin then Copy the path.
![How to set CLASSPATH in Java](https://static.tutorialandexample.com/java/how-to-set-classpath-in-java4.png)
5. After Clicking on new this pop-up will be displayed. You just have to add variable name and paste copied bin path as a variable value. Then click on Ok.
![How to set CLASSPATH in Java](https://static.tutorialandexample.com/java/how-to-set-classpath-in-java5.png)
6. Now you can see the path se set.
![How to set CLASSPATH in Java](https://static.tutorialandexample.com/java/how-to-set-classpath-in-java6.png)
7. Now you can verify it through the Windows terminal, the path is successfully set.
![How to set CLASSPATH in Java](https://static.tutorialandexample.com/java/how-to-set-classpath-in-java7.png)
Difference Between Class and CLASSPATH in Java
Sr. No | Class | CLASSPATH |
1. | A class is a fundamental concept in Java and is used as a blueprint or template to create objects. | The classpath is an environment variable used by the Java runtime environment (JRE) to locate and load classes and resources required by a Java program. |
2. | It defines the structure and behavior of objects by specifying the instance variables (fields) and methods that objects of that class possess. | It specifies the directories or JAR files where Java should search for the required class files and libraries during program execution. |
3. | Classes encapsulate data and functionality, providing a way to organize and modularize code. | The classpath enables the JVM to find and load the necessary classes, allowing the program to access their definitions and execute the code. |
4. | Instances of a class, known as objects, are created at runtime and can interact with each other by invoking methods or accessing variables defined within the class. | The classpath can be set as an environment variable (CLASSPATH) or specified explicitly when running a Java program using the -classpath or -cp command-line options. |