Why do we need Path and ClassPath? When using IDE's like eclipse we still need to add path ?
PATH is used by CMD prompt to find binary files. CLASSPATH is used by the compiler and JVM to find library files.
A Path can represent a root, a root and a sequence of names, or simply one or more name elements. A Path is considered to be an empty path if it consists solely of one name element that is empty. Accessing a file using an empty path is equivalent to accessing the default directory of the file system.
PATH values: notice how the directory we set for JAVA_HOME is the JDK installation root whereas for PATH we add the bin directory within the JDK installation. Take care to set these up correctly otherwise you'll have problems later on.
Path is used define where the system can find the executables(.exe) files and classpath is used to specify the location .
We don't need to set PATH and CLASSPATH to compile and run java program while using IDE like Eclipse.
These environment variables are required to compile and run java program using CMD.
Example-: Here is the screen shot of console to understand PATH and CLASSPATH quickly
Explanation-:
Compiling the program-
I have java program file Demo.java
stored at location D:\Programs\Classes
. Now I pointed location to D:\Programs\Classes
in CMD and executed javac Demo.java command. System will understand javac
with the help of PATH variable. Java program Demo.java
is complied successfully because PATH is set correctly to %JAVA_HOME%\bin
.
Running the program (class file)-
Since class file has been generated at the same location D:\Programs\Classes
, so we can run this class file by typing command java Demo as displayed in second line in the screenshot. Now system will find the class file with the help of CLASSPATH since my CLASSPATH variable has D:\Programs\Classes
path.
It's not required to point class file location in CMD to run it. System will understand java command with the help of PATH variable and find that class using CLASSPATH variable to run it.
let us clear the difference in points:
PATH
a) An environment variable which is used by the operating system to find the executables.
b) PATH is nothing but setting up an environment for operating system. Operating System will look in this PATH for executables.
c) Refers to the system
CLASSPATH
a) An environment variable which is used by the Java compiler to find the path, of classes i.e in J2EE we give the path of jar files.
b) Classpath is nothing but setting up the environment for Java. Java will use to find compiled classes.
c) Refers to the Developing Enviornment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With