Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Path and ClassPath in Java?

Why do we need Path and ClassPath? When using IDE's like eclipse we still need to add path ?

like image 736
Junaid Avatar asked Oct 11 '15 07:10

Junaid


People also ask

What is difference between path & CLASSPATH?

PATH is used by CMD prompt to find binary files. CLASSPATH is used by the compiler and JVM to find library files.

What is a path in Java?

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.

What is the difference between JAVA_HOME and path?

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.

Which of the following statement is correct difference between path and CLASSPATH?

Path is used define where the system can find the executables(.exe) files and classpath is used to specify the location .


2 Answers

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

enter image description here

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.

like image 183
Ashish Kumar Avatar answered Oct 15 '22 08:10

Ashish Kumar


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.

like image 26
N.Neupane Avatar answered Oct 15 '22 09:10

N.Neupane