C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programming language

What are the differences between path and classpath in java?

Learn: What are the differences between path and classpath variable in java? Explain in brief path and classpath variable in java?
Submitted by Preeti Jain, on February 03, 2018

1) PATH (path) in Java

1) path is an environment variable.

2) path describes the location(or place) where javac.exe (binary executables file) is located.

3) So whenever we try to compile and run a java program just before we have to set path variable of jdk\bin to run or compile a java program.

Example:

How to set path?

    D:\Java Articles> set path=C:\Program Files\Java\jdk1.8.0_111\bin;

How to check path is properly set or not?

D:\Java Articles> javac Usage: javac <options> <source_files>

Where, possible options include:
-g: Generate all debugging info,
-g:none: Generate no debugging info

What kind of error we will get if path is not set properly?

D:\Java Articles> javac

javac is not recognized as an internal or external command, operable program or batch file.

We will see what will happen if we don't set path variable or set wrong path of jdk\bin folder just before compiling or executing a java program. javac won't work.

Example:

D:\Java Articles> javac

"javac" is not recognized as an internal or external command, operable program or batch file

4) javac.exe file will be available in jdk\bin.

2) CLASSPATH (classpath) in Java

1) classpath is also an environment variable.

2) classpath describes the location where required (.class) file is located.

How to set classpath variable?

    D:\Java Articles > set classpath = C:\Program Files\Java\jre1.8.0_111\lib;

How to check classpath variable set or not (Run java file)?

1) If path sets properly then we will get output.

D:\Java Articles> java Classpath
welcome in java world

2) If path does not sets properly then we will get error (To run java program)

D:\Java Articles>java Classpath
Error: Could not find or load main class Classpath

3) Java compiler and JVM use classpath.

4) We will see what will happen if we don't set classpath variable or set wrong classpath of .class file then we may not compile and we may not run so both cases may arise sometimes it may not compile or sometime it may not run.

D:\Java Articles>java Classpath
Error: Could not find or load main class Classpath


Liked this article? Do share with your friends :)


COMMENTS