What is the difference between path and classpath in Java?

Assume, you have freshly installed Java on your computer and testing the installation if it has been properly installed. Usually we execute java -version command to check which version of java is installed on our system. But, when you run this command and get a message 'java' is not recognized as an internal or external command, operable program or batch file then understand that you have not added java installation folder to your syatem's PATH variable.

Once you add java installation folder to PATH variable then you will be able to execute java, javac and other commands from any location, otherwise you will have to go to java installation folder in order to execute java commands.

PATH is a system variable that contains locations of executable files or commands. All executable files and comands then can be executed from any location. Once we install java on our system we must add java installation folder to PATH variable because we will compile java programs from different directories, where they are located. In this case if PATH variable is not correctly set for java installtion directory we will not be able to execute javac coomand.

Like PATH, there is another variable of similar nature that is CLASSPATH. This variable is set to locate .class files while compiling java programs in case they have any dependency.

Both, PATH and CLASSPATH are environment variables and we need to set them while working with Java programming language. Following are the differences between them.

Path:

  • PATH is location of bin files (binary executable files). Example - java.exe, javac.exe.
  • OS uses the PATH variable to find the set of directories which contains executable files.
  • PATH setup environment for OS.
  • To set the PATH in Java, we need to include JDK_HOME/bin (in your case java installtion directoy may be differnt) directory in PATH environment variable.
  • PATH can not be overridden by any Java settings.
Classpath:
  • CLASSPATH is the location of the .class file, which is created after compiling the java source file.
  • Java classloader (or JVM) uses the CLASSPATH variable to find the location of classes and packages.
  • CLASSPATH setup environment for Java.
  • To set the CLASSPATH, we need to include all those directories where we have put either the .class file or JAR file which is required by the Java application.
  • CLASSPATH can be overridden by providing command line option -classpath or -cp to both "java" and "javac" commands or by using Class-Path attribute in Manifest file inside JAR archieve.


    • Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.