How To Set JAVA_HOME Environment Variable For All Users In Linux?


An environment variable is a variable whose value is set outside the program, Typically through a functionality built into the operating system. Generally, environment variables contains information such as drive, path, or filename. It can change the behavior of a program in the system. JAVA_HOME is an environment variable that points to the directory where the Java runtime environment (JRE) is installed on your computer.

In this article, we will show you how to set the JAVA_HOME variable correctly for all users in a Linux system.

How to install java?

First of all, you need to install java in your system, if you install an application that requires the java runtime environment. Use the following command to install it in your system.

First, update the package index of apt with-

sudo apt-get update

And then execute –

sudo apt-get install default-jdk

You can verify the installation with –

java --version

For more details, you can follow the java installation guide.

Find the location where java is installed

Now the application that requires java needs to know the location where java is installed. This path is commonly referred to as “JAVA_HOME”. The easiest way to check that location is by running this command –

sudo update-java-alternatives -l

This will display the following output in the terminal –

Copy the string that is highlighted in red.

Set JAVA_HOME environment variable

The /etc/profile is a file that contains Linux system-wide environment and startup programs. It is used to set the PATH variable, user limits, and other settings for the users. We will edit this file to set the JAVA_HOME variable. Now follow the below steps –

1. Open /etc/profile with a text editor –

sudo nano /etc/profile

2. Copy and paste the following at the end of this file –

export JAVA_HOME="path of java"
export PATH=$JAVA_HOME/bin:$PATH

Don’t forget to replace the “path of java”  with the string that you copied in the above section. In my case, I will paste the following in this file –

export JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH

Now, look into the image below –

3. Press ctrl+s to save in nano editor and then press ctrl+x to exit from it.

4. Logout and login again or reboot your system, to make changes effective.

Conclusion

The method discussed in this article is among the easiest ones. Now, I hope this article is useful to you. If you would like to say something on this topic you can share your thoughts with us.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.