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

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


This post was last updated on August 1st, 2020 at 12:51 pm

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.


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.

Previous How To Install Ubuntu 20.04 LTS?
Next Static Hosting With AWS and user-friendly URLs

About author

You might also like

System Admin 0 Comments

Logical Volume Manager(LVM)

This post was last updated on August 3rd, 2020 at 02:12 pmLVM simply stands for “Logical Volume Manager”. It is a tool for logical volume management. LVM is an advanced

How To 0 Comments

How To Add a User During Kickstart

This post was last updated on May 26th, 2020 at 10:37 pm Adding a user during kickstart: /usr/sbin/useradd username /usr/sbin/usermod -p ‘<encrypterd_password>’ <username> Making encrypted passwords: $ openssl passwd -1

How To 0 Comments

How To Check Current Version Of Ubuntu Using lsb_release Command?

This post was last updated on August 1st, 2020 at 12:40 pmUbuntu comes in two releases the first one is LTS and the second is standard versions. LTS (Long Term


No Comments Yet!

You can be first to comment this post!

Leave a Reply