Open In App

How to Set Java Path in Windows and Linux?

Last Updated : 17 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

PATH is an environment variable that is used by Operating System to locate the exe files (.exe) or java binaries ( java or javac command). The path once it is set, cannot be overridden. The PATH variable prevents us from having to write out the entire path to a program on the Command Line Interface every time we run it. Moreover, the path is just a variable that stores a bunch of shortcuts.

To execute Java console-based programs in Windows or Linux environments, we have to use Java and javac commands. The commands java and javac are not known to the operating system as we don’t specify where the executables reside. Hence, we need to specify the path where the executables are located. This is the reason we set the path and specify the path of the bin folder because the bin contains all binary executable files. After setting the path, it can load all necessary items in the program including the compiler or interpreter itself. 

Below is the procedure for how to set Java Path in both Windows and Linux?

To Set Java Path in Windows

Step 1: Go to the Search box and type Advanced System settings in it. Now click on the View Advanced System settings.

to-set-java-path-in-windows

Step 2: Select the Advanced tab and then click environment variables.

to-set-java-path-in-windows

Step 3: In the system, variables click the New button. Now in the edit System variable, type variable name as JAVA_HOME and variable path as the path where the JDK folder is saved and click on OK button Usually the path of the JDK file will be C:\Program Files\Java\jdk1.8.0_60.

to-set-java-path-in-windows

Step 4: Now in the system variables go to the path and click the Edit button.

to-set-java-path-in-windows

Step 5: Click the New button.

to-set-java-path-in-windows

Step 6: Now add the following path: %JAVA_HOME%\bin

to-set-java-path-in-windows

To Find Java Path in Windows:

Here are the simple steps to find your Java path in Windows:

Step 1: Open a Command Prompt window.

Type the following command and press Enter:

where java

Step 2: You should see the path to your Java installation printed in the Command Prompt. It will typically look something like this:

C:\Program Files\Java\jdk1.8.0_291\bin\java.exe

That’s it! You’ve found your Java path in Windows.

To Set Java Path in Linux

Follow the given steps to set Java Path in Linux –

  • Open the terminal and enter the following command: 
sudo nano /etc/environment.
  • A file will be opened and add the following command to that file:
JAVA_HOME = "YOUR_PATH". 
  • Replace YOUR_PATH with the JDK bin file path.
  • Now restart your computer or virtual machine that you are using (or) reload the file: source /etc/environment
  • You can test the path by executing
 echo $JAVA_HOME
  • If you get the output without any error, then you’ve set the path correctly.
  • If you get any errors, try repeating the procedure again.

To Find Java Path in Linux:

Here are the steps to find Java Path in Linux System –

Step 1: Open a Terminal window.

Step 2: Type the following command and press Enter:

which java

Step 3: You should see the path to your Java installation printed on the terminal. It will typically look something like this:

/usr/bin/java

That’s it! You’ve found your Java path in Linux.

Conclusion

So, this was all about how you can find and set Java path in Windows and Linux. If you also have other queries regarding setting Java Path, Java_Home variable and default Java path, You can also refer to the following frequently asked questions.

Set Java Path in Windows and Linux – FAQs

How to setup java_home variable in Linux?

  • Open the terminal.
  • Edit the /etc/environment file using the command sudo nano /etc/environment.
  • Add the line JAVA_HOME=”YOUR_PATH”, replacing “YOUR_PATH” with the JDK bin file path.
  • Restart your computer or reload the file with source /etc/environment to apply the changes.
  • Verify the setup by executing echo $JAVA_HOME in the terminal.

How do I find my java path in Linux?

To find the Java path in Linux, open a terminal window and use the command which java. This will display the path to your Java installation, typically located at /usr/bin/java.

What is the Default Java Path in Windows?

The default Java path in Windows can be found by opening a Command Prompt window and typing where java. This will show the path to the Java installation directory, such as C:\Program Files\Java\jdk1.8.0_291\bin\java.exe.

How to check if Java is Installed?

To check if Java is installed, you can run java -version in the Command Prompt or terminal. If Java is installed, it will display the installed version information. If not, it will prompt you to install Java.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads