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?
By Preeti Jain Last updated : March 25, 2024
PATH (path) in Java
- path is an environment variable.
- path describes the location(or place) where javac.exe (binary executables file) is located.
- 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.
Setting path in Java
D:\Java Articles> set path=C:\Program Files\Java\jdk1.8.0_111\bin;
Checking 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
javac.exe file will be available in jdk\bin.
CLASSPATH (classpath) in Java
- classpath is also an environment variable.
- classpath describes the location where required (.class) file is located.
Setting classpath variable in Java
D:\Java Articles > set classpath = C:\Program Files\Java\jre1.8.0_111\lib;
Checking 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
Differences between path and classpath in Java
The following table shows the differences between path and classpath in Java:
Feature |
Path |
Classpath |
Definition |
The path refers to the location of directories. |
The classpath refers to the location of directories and JAR files. |
Usage |
The path is used to specify the location of executable files. |
The classpath is used to specify the location of Java classes and resources. |
Environment |
The path is system-specific. |
The classpath is Java-specific. |
Configuration |
The path sets using the PATH environment variable. |
The classpath sets using the CLASSPATH environment variable. |
Separators |
The path uses system-specific separators (e.g., colon : in Unix-like systems, semicolon ; in Windows). |
The classpath uses platform-independent separators (e.g., colon :). |
Priority |
The path system takes precedence over classpath. |
The Classpath takes precedence over system path. |
Functionality |
The path determines where the system looks for executable files. |
The classpath determines where the Java Virtual Machine (JVM) looks for classes and resources. |
Impact |
The path affects the execution of any executable files. |
The classpath affects the loading of Java classes and resources. |
Examples |
The path specifies where to find system executables (e.g., java, javac). |
The classpath specifies where to find Java libraries and classes. |
Modification |
The path is typically modified by system administrators or developers. |
The classpath is modified by developers to include required dependencies. |