4 Difference between PATH and CLASSPATH in Java

PATH and CLASSPATH are the two most common environment variables you will deal with while configuring java environment. The sad part is most of the java developers are unaware of the concept of PATH and CLASSPATH. So today, in this tutorial I will share what is PATH and what is CLASSPATH along with difference between them. Also, I will let you know how to set PATH and CLASSPATH in windows and linux/unix.

Read Also :  4 Difference between NoClassDefFoundError and ClassNotFoundException



What is PATH

PATH is an environment variable which is used by Operating System to locate the exe files (.exe) or java binaries ( java or javac command).

What is CLASSPATH

CLASSPATH is also an environment variable which is used by Application ClassLoader to locate and load the .class files . CLASSPATH setting is done to locate the .class files found in another directory.

Difference between PATH and CLASSPATH in java


1. Definition:  PATH is an environment variable. It is used by operating system to find the executable files (.exe) or in other words java binaries (java or javac).
CLASSPATH is also an environment variable used by Application ClassLoader to locate and load the .class files(compiled java bytecodes).

2. Setting :  In order to set PATH ,  JDK_HOME/bin directory needs to be included in PATH environment variable.
In order to set CLASSPATH, you need to include all the directories which contain .class files and jar files  as required by the application.  

3. Overridden : PATH environment variable once set , its value can not be overridden via java settings. 
CLASSPATH environment variable can be overridden by using the command line option -cp or -classpath to both javac and java commands.

4. Usage :  PATH is used by operating system(Windows and Linux) to execute command in the shell or to find any binary.
CLASSPATH is only used by Application CLASSLOADER to load class files.
  
Above are the main points, you need to remember to solve errors like NoClassDefFoundError and ClassNotFoundException. Now I will share how to set PATH and CLASSPATH in Windows and Linux/Unix.


How to Set PATH and CLASSPATH in Windows and Linux/Unix


Command to set PATH and CLASSPATH in Windows 

 PATH :       set PATH=%PATH%;C:\Program Files\Java\JDK1.8.20\bin

 CLASSPATH :   
set CLASSPATH=%CLASSPATH%;C:\Program Files\Java\JDK1.8.20\lib


Command to set PATH and CLASSPATH in Linux/Unix


 PATH :        export PATH=${PATH}:/opt/Java/JDK1.8.20/bin

 CLASSPATH :   
export CLASSPATH=${CLASSPATH}:/opt/Java/JDK1.8.20/lib

Points To Remember About CLASSPATH :



1. The default value of CLASSPATH is dot "." which means only current directory is searched.If you specify the CLASSPATH variable or -cp command line switch overrides this value.


2. If CLASSPATH finds a class which is present in current directory then it will load and use it , irrespective of the same name class presents in the another directory which is also included in the CLASSPATH.  

Recap  : Difference between PATH and CLASSPATH in Java 




PATHCLASSPATH
DefinitionOperating system looks for executable files(.exe) or java binaries (java or javac command)Application ClassLoader used to load and locate the .class files and jar files  as required by the application.
OverriddenPATH once set can not be overridden through java settingsCLASSPATH values can be overridden by using the command line option -cp or -classpath to both java and javac commands.
UsagePATH is used by Operating SystemCLASSPATH is used by Application ClassLoader
SettingIn order to set PATH ,  JDK_HOME/bin directory needs to be included in PATH environment variable.In order to set CLASSPATH, you need to include all the directories which contain .class files and jar files  as required by the application.

You can check out the following article to set PATH in Windows(XP, Vista and Windows 7) .

Please mention in comments if you have any questions regarding PATH and CLASSPATH.

About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry