When working with Java applications, you may have encountered the terms “path” and “class path” in relation to setting up your development environment. Understanding the difference between these two variables is essential for efficiently running and compiling your Java programs.
In simple terms, the “path” variable specifies the directories where the operating system looks for executables, such as compilers and launchers. On the other hand, the “class path” variable informs Java applications and JDK tools about the location of user-defined classes and libraries.
Difference between Path and Class Path:
- Path: The “path” variable is used to specify the directories where the operating system looks for executable files. It is used to run executables conveniently without specifying the full path each time.
After installing JDK, you will get a bin folder in your drive. (C:\Java\jdk1.8.0\bin). In this bin folder contain executables like javac.exe, java.exe, javadoc.exe etc. every time we have to run an executable, we need to specify the full path to the executable. for example, to compile a file using javac command we have to specify the complete path of javac.exe (C:\Java\jdk1.8.0\bin\javac MySampleClass.java).
By setting the path environment variable, we can enhance the convenience of executing executables from any directory without the need to provide the full path for each command (javac MySampleClass.java).
- Class Path: The “class path” variable is used by Java applications and JDK tools to locate user-defined classes and libraries. It specifies the directories or JAR files where Java bytecode (.class) files are located.
Setting the Path Variable (Windows):
To set the path environment variable in Windows, follow these steps:
- Open the Start menu and search for “Environment Variables.”
- Select “Edit the system environment variables.”
- Click the “Environment Variables” button.
- In the “System Variables” section, locate the “Path” variable and click “Edit.”
- Add the directories containing the executables you want to run. Separate multiple directories with a semicolon (;).
Eg:- (C:\Java\jdk1.8.0\bin;)
- Click “OK” to save the changes.
Setting the Path Variable (Linux):
To set the path environment variable in Linux, you can modify the .bashrc file. Here’s an example:
- Open a terminal.
- Navigate to your home directory: cd ~
- Open the .bashrc file in a text editor: vi .bashrc
- Add the following line at the end of the file, replacing <directory> with the desired directory: export PATH=$PATH:<directory>
- Save the file and exit the text editor.
- Run the command source .bashrc to apply the changes.
Setting the Class Path Variable (Windows and Linux): To set the class path environment variable, you can follow similar steps as setting the path variable. Here are the steps in brief:
- Open the environment variables settings as mentioned above.
- Locate the “CLASSPATH” variable (or create a new one if it doesn’t exist).
- Add the directories or JAR files containing the Java bytecode (.class) files you want to include. Separate multiple entries with a semicolon (;).
- Click “OK” to save the changes.
To verify whether the CLASSPATH is set on Microsoft Windows, you can use the following command in the command prompt:
C:> echo %CLASSPATH%
Executing this command will display the current value of the CLASSPATH variable, if it is set.
To check whether the CLASSPATH is set in Solaris or Linux, you can execute the following command in the terminal:
$ echo $CLASSPATH
By running this command, you will see the current value of the CLASSPATH variable, if it is set.
Leave a Reply