To use certain tools like javac and java, it is necessary to set the path. If you save your Java source file within the JDK/bin directory, you won’t need to set the path as all the required tools will be available in the current directory. However, if your Java file is located outside of the JDK/bin folder, setting the path of the JDK becomes necessary. This is because the system needs to know where to find the required tools to execute the Java code properly. Therefore, it’s important to ensure that the path is set correctly to avoid any errors when compiling or running your Java program.
There are two ways to set the path in Java:
- Temporary
- Permanent
1) How to set the Temporary Path of JDK in Windows
- Open the command prompt.
- Copy the path of the JDK/bin directory.
- In the command prompt, type “set path=” and paste the copied path after the equals sign.
- Press enter to set the path temporarily.
By following these steps, you can set the temporary path of JDK and access the necessary tools for compiling and executing Java programs from anywhere on your system. However, this path will only be valid for the current command prompt session and will be reset once you close it. To set the path permanently, you will need to modify the system environment variables.
For Example:
set path=C:\Program Files\Java\jdk1.6.0_23\bin
2) How to set the Permanent Path of JDK in Windows
To set the permanent path of JDK, you can follow these steps:
- Open “My Computer” and right-click to select “Properties”.
- Go to the “Advanced” tab and click on the “Environment Variables” button.
- In the “User variables” section, click the “New” button.
- Enter “path” as the variable name and the path of the JDK/bin folder as the variable value.
- Click “OK” to save the variable.
- Click “OK” again to close the Environment Variables window.
- Click “OK” once more to close the System Properties window.
By following these steps, you can set the permanent path of JDK and access the necessary tools for compiling and executing Java programs from any location on your system. This path will be available for all command prompt sessions and will not be reset unless modified again in the future.
For Example:
1. Open “My Computer” and right-click to select “Properties”.
2. Go to the “Advanced” tab and click on the “Environment Variables” button.
3. In the “User variables” section, click the “New” button.
4. Enter “path” as the variable name and the path of the JDK/bin folder as the variable value and click “OK”. and Click “OK” again to close the Environment Variables window.
5. Click “OK” once more to close the System Properties window.
Now your permanent path is set. You can now execute any program of Java from any drive.
How to Set Java Path in Linux OS
The process for setting the path in Linux OS is similar to that of Windows OS, but it involves using the “export” tool instead of “set”. Here are the steps to set the path in Linux OS:
- Open the terminal window.
- Navigate to your home directory.
- Open the “.bashrc” file using a text editor such as nano or vi.
- Add the following line to the file: “export PATH=$PATH:/path/to/jdk/bin” (replace “/path/to/jdk/bin” with the actual path to the bin folder of your JDK installation).
- Save and close the file.
- To apply the changes, either logout and log back in or run the command “source ~/.bashrc”.
By following these steps, you can set the path in Linux OS and access the necessary tools for compiling and executing Java programs from anywhere in the terminal. The “export” tool makes the path available to all terminal sessions, much like the permanent path setting in Windows OS.