Difference between PATH and CLASSPATH
Are you confused between path and classpath in java?
Give this article a read, it will surely clear all your confusions
What is PATH?
What is CLASSPATH?
The main difference between PATH and CLASSPATH is that Path is set for java tools in java programs like java and javac, which are used to compile your code. Whereas CLASSPATH? is used by System or Application class loader to locate and load compile Java bytecodes stored in the .class file.
Rt.jar file contains all classes and the bin folder contains all tools, so we set PATH up to bin folder and set CLASSPATH up to lib folder, as shown in the above picture.
Difference between path and classpath on various parameters:-
1 . Definition?
? - It is used by the operating system to locate executable files (.exe) and java binaries.
- It is used by the application class loader to load the .class files(compiled java codes).
2. Setting
- To set PATH in Java, you need to include the JDK_HOME/bin directory in the PATH environment variable.
- 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. Overridden
- Path once set cannot be overridden by java settings.
- Once you install Java on your machine, it is necessary to set the PATH environment variable so that executables (javac.exe,java.exe,javadoc.exe) run smoothly from any directory without needing to type the entire path.
Example:-
? When the path variable is set
?????C:\javac Test.java
When path variable is not set
C:\Java\jdk1.7.0\bin\javac Test.java
领英推荐
- Classpath values can be overridden by using the command-line option -cp or -classpath to both java and javac commands.
- Java compiler and JVM use CLASSPATH to determine the location of required class files.
Example:-
C:\Program Files\Java\jdk1.6.0\bin
4 . Usage
-PATH is used by the operating systems (Windows and Linux) to execute commands in the shell or to find any binary.
-CLASSPATH is used by Application CLASSLOADER to load class files
How to set PATH and CLASSPATH in Windows and Unix
Both PATH and CLASSPATH are environment variables and they can be set using
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
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
Thank you, Hope this article would have helped you. Suggestions are welcome.