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.

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.

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

About author

You might also like

Linux Topics 0 Comments

How To Switch Users In A Linux Or Unix Terminal?

This post was last updated on August 1st, 2020 at 02:42 pmLinux/Unix is a multi-user operating system that means more than one user can use system resources simultaneously. In order

Linux Topics 0 Comments

How Manually Partition A Disk During Installation of Ubuntu?

This post was last updated on August 1st, 2020 at 12:16 pmA partition is nothing but a logical division of a physical disk. Partition makes it easy to organize and

Linux Terminal 0 Comments

How To Change Username In Linux/Unix From The Terminal?

This post was last updated on August 1st, 2020 at 03:04 pmIn a Linux/Unix system sometimes you may need to change the username or User ID (UID) of a user

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply