What is PATH and CLASSPATH in Java
The PATH and CLASSPATH are the two most important environment variables of the Java environment which are used to find the JDK binaries used to compile and run Java in windows and Linux and class files which are compiled Java bytecodes. From my personal experience I can say that PATH and CLASSPATH are the two most problematic things for beginners in Java programming language due to two reasons; first because in most Java courses nobody tells details of what is a PATH and CLASSPATH, What do PATH and CLASSPATH do, What is meaning of setting PATH and CLASSPATH, What happens if we do not set them, Difference between PATH vs CLASSPATH in Java or simply How Classpath works in Java, etc.
The PATH and CLASSPATH are the two most important environment variables of the Java environment which are used to find the JDK binaries used to compile and run Java in windows and Linux and class files which are compiled Java bytecodes. From my personal experience I can say that PATH and CLASSPATH are the two most problematic things for beginners in Java programming language due to two reasons; first because in most Java courses nobody tells details of what is a PATH and CLASSPATH, What do PATH and CLASSPATH do, What is meaning of setting PATH and CLASSPATH, What happens if we do not set them, Difference between PATH vs CLASSPATH in Java or simply How Classpath works in Java, etc.
These basic question which answers most of the details about PATH and CLASSPATH in Java are mostly not answered until Java programmer itself acquire this knowledge, Things may be changed nowadays but important of PATH and CLASSPATH is still high.
The most common cause of dreaded error like java.lang.NoClassDefFoundError and java.lang.ClassNotFoundException is either incorrect or misconfigured CLASSPATH in Java.
In this article, I'll tell you about the practical difference between PATH and CLASSPATH environment variables, where are they located, and how exactly they are used by the Java compiler and JVM. Once you know this basic detail, you would be able to solve most of the classpath-related problems by yourself.
1. The main difference between PATH and CLASSPATH is that PATH is an environment variable that is used to locate JDK binaries like the "java" or "javac" command used to run java program and compile the java source file. On the other hand, CLASSPATH, an environment variable is used by System or Application ClassLoader to locate and load compile Java bytecodes stored in the .class file.
2. In order to set PATH in Java, you need to include JDK_HOME/bin directory in PATH environment variable while in order to set CLASSPATH in Java you need to include all those directories where you have put either your .class file or JAR file which is required by your Java application.
3. Another significant difference between PATH and CLASSPATH is that PATH can not be overridden by any Java settings but 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 archive.
4. PATH environment variable is used by the operating system to find any binary or command typed in the shell, this is true for both Windows and Linux environments while CLASSPATH is only used by Java ClassLoaders to load class files.
These were some notable differences between PATH vs CLASSPATH in Java and they are worth remembering to debug and troubleshoot Java-related issues. Though, I highly recommend you to join these best Java Programming courses to build your fundamentals in Java.
In this article, I'll tell you about the practical difference between PATH and CLASSPATH environment variables, where are they located, and how exactly they are used by the Java compiler and JVM. Once you know this basic detail, you would be able to solve most of the classpath-related problems by yourself.
Difference between PATH and CLASSPATH in Java
Here are some of the common differences between PATH vs CLASSPATH in Java :1. The main difference between PATH and CLASSPATH is that PATH is an environment variable that is used to locate JDK binaries like the "java" or "javac" command used to run java program and compile the java source file. On the other hand, CLASSPATH, an environment variable is used by System or Application ClassLoader to locate and load compile Java bytecodes stored in the .class file.
2. In order to set PATH in Java, you need to include JDK_HOME/bin directory in PATH environment variable while in order to set CLASSPATH in Java you need to include all those directories where you have put either your .class file or JAR file which is required by your Java application.
3. Another significant difference between PATH and CLASSPATH is that PATH can not be overridden by any Java settings but 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 archive.
4. PATH environment variable is used by the operating system to find any binary or command typed in the shell, this is true for both Windows and Linux environments while CLASSPATH is only used by Java ClassLoaders to load class files.
These were some notable differences between PATH vs CLASSPATH in Java and they are worth remembering to debug and troubleshoot Java-related issues. Though, I highly recommend you to join these best Java Programming courses to build your fundamentals in Java.
How to set PATH and CLASSPATH in Windows and Unix?
If you are familiar with DOS operating system and how to use command prompt in Windows or shell in Linux setting PATH and CLASSPATH is a trivial exercise. Both PATH and CLASSPATH are environment variables and can be set using the export command in Linux and using set keyword in DOS and Windows as shown below:Command to set PATH in Windows
set PATH=%PATH%;C:\Program Files\Java\JDK1.6.20\bin
Command to set PATH in UNIX/Linux
export PATH = ${PATH}:/opt/Java/JDK1.6.18/bin
Look at the difference between the two commands, in Linux use a colon(:) as a separator, and Windows uses a semi-colon(;) as a separator.
Command to set CLASSPATH in windows
set CLASSPATH=%CLASSPATH%;C:\Program Files\Java\JDK1.6.20\lib
Command to set CLASSPATH in Unix/Linux
export CLASSPATH= ${CLASSPATH}:/opt/Java/JDK1.6.18/lib
Also, don't forget to include the current directory, denoted by a dot(.) to include in CLASSPATH, this will ensure that it will look first in the current directory and if it found the class it will use that even if that class also exists in another directory which exists in CLASSPATH.
If you using Windows 8 or Windows 10 then you can also follow the steps given here in this article to set up the JAVA_HOME, PATH, and CLASSPATH for Java programs.
It will look something like this in Windows 10 Operating System :
In short, PATH is used by operating system to find the Java binaries. For example, when you type "java" in command line then OS search for java.exe or equivalent Linux executable to execute the Java runtime. On the other hand, Classpath is used to locate the class files which your Java program need. If any class is not find in the Classpath then ClassNotFoundException or NoClassDefFoundError is thrown.
Other Java setup related articles for beginners:
Thanks for reading this article so far. If you found my explanation of PATH and ClassPath useful and you are able to understand the difference between PATH and Classpath then please share this tutorial with your friends and colleagues. If you have any questions, feel free to ask.
It will look something like this in Windows 10 Operating System :
Other Java setup related articles for beginners:
- How to set PATH in Mac OS X 10.10 Yosemite? (guide)
- How to fix 'javac' is not recognized as an internal or external command? (solution)
- How to set the JAVA_HOME environment variable in Java? (guide)
- How to set java.library.path in Eclipse IDE (guide)
- How to run a HelloWorld Java program from command prompt (guide)
- How to debug a Java program in Eclipse IDE (guide)
Best explanation of ClassPath and Path I ever read , Thanks for this simple yet informative tutorial on Java.
ReplyDeleteNice Explanation Sir.This is really very beneficial for beginners .
ReplyDeleteHye javin.
ReplyDeleteI would like to ask. What does the last line means?
I Love u man. U are simply Great
ReplyDeleteJava uses computer file system directories to store packages. For example, the .class files for any classes you declare to be part of Wisen must be stored in a directory called Wisen.
ReplyDeleteJava is case sensitive. Therefore, the directory name must match exactly the package name even in Windows operating system.
Java Training In Chennai
Thats great tutorial... Many many thanks to you Sir.. This help me a lot..
ReplyDeleteHello, World
ReplyDeleteException in thread "main" java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
at FileHelper.getFilePathToSave(FileHelper.java:19)
at FileHelper.main(FileHelper.java:33)
Nice explaination.
ReplyDeleteI am using JDK 7.I have set PATH but not set CLASSPATH and thing are working fine.
Question is if we don't set CLASSPATH explicitly what is assumed by java ?
export CLASSPATH= ${CLASSPATH}:/opt/Java/JDK1.6.18/lib
ReplyDeletethe space following = should be removed
@ : Question is if we don't set CLASSPATH explicitly what is assumed by java ?
ReplyDeleteFor running java programs it not necessary to set CLASSPATH. It is necessary only when you use classes from third party .jar files. Here in this example I am taking the example of JDBC driver for MySQL.
The current directory
DeleteThank you,nice way of description of Difference between Path and Class Path. It is one of the most frequently asked question in java For Freshers. Setting Path for File JDK Software and Setting Classpath for Jars Class names of Servlet Struts, Hibernate, Spring
ReplyDeleteThank you for your great explanation.
ReplyDeletewhy do u need to set ClassPath when u use -cp option?
ReplyDeleteNow I got to know the clear concept of PATH and CLASSPATH. Quite informative!
ReplyDeleteCreate a class called Rectangle that includes two pieces of information as instance
ReplyDeletevariables a length (type integer) and a breadth (type integer). Your class should
have a constructor that initializes the two instance variables. Your class should
also have two methods called calcArea and calcPerimeter which calculates the
area and perimeter of the rectangle respectively and display their corresponding
calculated values. Create the object of the Rectangle class and then call the
calcArea method.
Do your homework yourself brev.
Deletenice explanation ,really aprecitaed
ReplyDelete"export CLASSPATH= ${CLASSPATH}:/opt/Java/JDK1.6.18/lib" - please remove space after "="
ReplyDeleteWonderfull..
ReplyDeleteSplendid articlee..:)
Keep doing.!
Superb ..............really loved it as a beginner....:)
ReplyDeleteBest explanation in simple terms....worth reading
ReplyDeleteThank you Riya, Anonymous and others, glad you like this article about PATH vs CLASSPATH differences. Don't forget to share with your friends, it makes a lot of difference.
DeleteNice Explaination. Keep it up.
ReplyDeleteVery nice explanation...
ReplyDeletevery nice explaination
ReplyDeleteI want to mention that classpath can also include other files besides .class types. For example, classpath is also where Spring applications look for .properties files.
ReplyDeleteHello @Brandon, that's true, not only Spring but Hibernate and other framework also look for XML config file and properties file in Classpath. There are also methods to load any resource e.g. image or text file from classpath in JDK.
DeleteVery nice and simple explaination of the most confused concept in java.Great
ReplyDeleteThank you Swati, happy that you find the information about PATH and class path useful.
Deletepath is used for compiling the code ,you want link the database that time you want jars ,that time you set the classpath
ReplyDeletewhy do we set PATH envinorment variable
ReplyDeleteYou set PATH enviornment variable to type command in the command prompt like cmd on Windows or shell on Linux. When you type something on command prompt, OS search for it in PATH enviornment variable. For example, if you have java.exe in PATH then when you type java on command prompt, it will execute that executable. This is required to run any java application hence you set path.
Deleteplease correct me if I am wrong but I wouldn't refer to PATH as java environment variable. I think it is OS-wide environment variables which specifies where command line will look for executables when you specify them without full path. so it allows you to do, "myProgram" command instead of "C:\myCoolPrograms\myProgram.exe". because it is true for any executable, it is also true for java and allows us to compile, for example, with "javac myClass" instead of "C:\Programs\myJavaDistribution\bin\javac.exe myClass"
ReplyDeleteand... I've just come to the place in your article where you say exactly what I have commented a couple of minutes ago. sorry, sir. Great great job btw! Thank you so much for this
ReplyDelete