How to set a permanent path of JDK in Windows
If you want to set the Java path permanently in Windows, follow these steps:
1. Right-click on the "My Computer" or "This PC" icon & select "Properties".
2. Click on "Advanced system settings" located on the left sidebar.
3. In the System Properties window, click on the "Environment Variables" button.
4. Under "System variables", scroll down & locate the "Path" variable, then click on "Edit".
5. Click on "New" & add the path to your JDK's `bin` directory. For example:
C:\Program Files\Java\jdk1.8.0_202\bin
Make sure to replace `C:\Program Files\Java\jdk1.8.0_202` with the actual path to your JDK installation directory.
6. Click "OK" to save the changes in all the windows.
7. Open a new Command Prompt & type `javac -version` to verify that the path is set correctly. You should see the version of the Java compiler installed on your system.
Let’s look at an example of how the Path variable should look after adding the JDK path:
PATH = C:\Windows\system32;C:\Windows;C:\Program Files\Java\jdk1.8.0_202\bin
By setting the permanent path, you ensure that the Java compiler & other tools are accessible from any Command Prompt session without the need to set the path manually each time.
Setting Java Path in Linux OS
In Linux, setting the Java path is a bit different compared to Windows. Here's how you can set the Java path in Linux:
1. Open the terminal by pressing `Ctrl + Alt + T` or by searching for "Terminal" in the application menu.
2. Open the `.bashrc` file in a text editor using the following command:
nano ~/.bashrc
3. Scroll down to the end of the file & add the following lines:
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export PATH=$PATH:$JAVA_HOME/bin
Replace `/usr/lib/jvm/java-8-openjdk-amd64` with the actual path to your Java installation directory.
4. Save the changes by pressing `Ctrl + X`, then `Y`, & finally `Enter`.
5. Reload the `.bashrc` file by running the following command in the terminal:
source ~/.bashrc
6. To verify that the Java path is set correctly, type `javac -version` in the terminal & press Enter. You should see the version of the Java compiler installed on your system.
Here's an example of how the `.bashrc` file should look after adding the Java path:
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User-specific aliases and functions
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export PATH=$PATH:$JAVA_HOME/bin
By adding the Java path to the `.bashrc` file, you ensure that the Java compiler & other tools are accessible from any terminal session. The `.bashrc` file is automatically loaded every time you open a new terminal, so you don't need to set the path manually each time.
Frequently Asked Questions
What is the difference between setting a temporary & permanent Java path in Windows?
Setting a temporary Java path is valid only for the current Command Prompt session, while setting a permanent path ensures that Java is accessible from any Command Prompt session without the need to set the path manually each time.
Can I set multiple Java paths in the Path variable in Windows?
Yes, you can add multiple paths to the Path variable. Each path should be separated by a semicolon (;).
How can I verify that the Java path is set correctly in Linux?
To verify the Java path in Linux, open a terminal & type javac -version. If the path is set correctly, you should see the version of the Java compiler installed on your system.
Conclusion
In this article, we learned how to set the Java path in both Windows & Linux operating systems. We covered setting the temporary & permanent path of JDK in Windows, as well as setting the Java path in Linux by modifying the .bashrc file. By properly configuring the Java path, you ensure that the Java compiler & other necessary tools are accessible from the command line, allowing you to develop & run Java applications seamlessly.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.