Table of contents
1.
Introduction
2.
How to set the Temporary Path of JDK in Windows
3.
How to set a permanent path of JDK in Windows
4.
Setting Java Path in Linux OS
5.
Frequently Asked Questions
5.1.
What is the difference between setting a temporary & permanent Java path in Windows?
5.2.
Can I set multiple Java paths in the Path variable in Windows?
5.3.
How can I verify that the Java path is set correctly in Linux?
6.
Conclusion
Last Updated: Jul 23, 2024
Easy

How to Set Path in Java?

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Java is a popular programming language used for developing various applications. When working with Java, setting the path is an essential step. The Java path tells your operating system where to find the Java compiler and other necessary tools. 

How to Set Path in Java

In this article, we will learn how to set the Java path in both Windows and Linux operating systems. 

How to set the Temporary Path of JDK in Windows

Setting the temporary path of JDK (Java Development Kit) in Windows is a straightforward process. Here's how you can do it:

1. Open the Command Prompt by pressing the Windows key + R, typing "cmd", & pressing Enter.
 

2. In the Command Prompt, type the following command & press Enter:

   set path=C:\Program Files\Java\jdk1.8.0_202\bin
   Replace `C:\Program Files\Java\jdk1.8.0_202` with the actual path to your JDK installation directory.


3. To verify that the path is set correctly, type `javac -version` & press Enter. You should see the version of the Java compiler installed on your system.

Let’s look at an example of how it should look:

C:\>set path=C:\Program Files\Java\jdk1.8.0_202\bin

 

C:\>javac -version
javac 1.8.0_202

 

Setting the temporary path is useful when you want to use a specific version of Java for a particular project or session. However, keep in mind that this path will only be valid for the current Command Prompt session. If you close the Command Prompt & open it again, you'll need to set the path again.

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 AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass