How to Set JAVA_HOME Path in Ubuntu 20.04 and Ubuntu 22.04

Many Java-based programs require the Java Runtime Environment (JRE) to run smoothly regardless of the operating system. For development, most IDEs like Eclipse and NetBeans require the Java Development Kit (JDK) to be installed on your computer. Whether you're a newbie who wants to learn how to develop or an application you have installed requires a Java installation on your system, we've got you covered. Setting up the JAVA_HOME path is quite simple.

In this tutorial, we will learn how to install the JDK on Ubuntu 20.04 and Ubuntu 22.04 and set up the Java_HOME path. Let's start with the installation of the JDK.

Install OpenJDK on Ubuntu

Note: Make sure that you have updated the Advanced Package Tool (APT) before you move forward to install OpenJDK.

Press Ctrl + Alt + T to open the terminal and enter the following command mentioned in the box. You can either install OpenJDK 8 or the newer versions OpenJDK 11 to OpenJDK 18. Not all versions are available on any Ubuntu version. You can check for available versions with the command:

apt-cache search openjdk-

Install OpenJDK 8

sudo apt install openjdk-8-jdk

Install OpenJDK 11

sudo apt install openjdk-11-jdk

Install OpenJDK 14

sudo apt install openjdk-14-jdk

Install OpenJDK 16

sudo apt install openjdk-16-jdk

Install OpenJDK 17

sudo apt install openjdk-17-jdk

Install OpenJDK 18

sudo apt install openjdk-18-jdk

You will be prompted to enter your sudo password to continue with the installation.

Install OpenJDK on Ubuntu Linux

Once you’ve entered that, wait for the system to finish the installation and move on to step 2.

Set JAVA_HOME Path

All you have to do now is to set the “JAVA_HOME” and “PATH” environment variables, and then you are done. Enter the following commands to set your environment variables. Ensure that your environment variables point to a valid installation of JDK on your machine. For Ubuntu 18.04, the path is /usr/lib/jvm/java-8-openjdk-amd64/

export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64

To check whether your JAVA_HOME path has been successfully saved, enter the following command to check.

echo $JAVA_HOME

The value stored in the JAVA_HOME variable will be displayed on the terminal as you can see in the screenshot

Ubuntu JAVA_HOME Path

Add JAVA bin directory to the PATH variable

Like we have added JAVA_HOME path, we will now update the PATH variable as well. To do that, enter the following command on the terminal.

export PATH=$PATH:$JAVA_HOME/bin

This will append the java bin directory to the existing PATH variable. You can also check the PATH variable by entering the following command

echo $PATH

Ubuntu Java Binary Path

Test JAVA setup

You have successfully installed OpenJDK on your machine. You can verify your installation by entering the following command on your terminal.

java -version

Check Java version

Through this simple tutorial, you have now configured the JAVA_HOME variable in your operating system. You can now easily run java based applications as well as development environments on your machine.

Frequently Asked Questions About JAVA_HOME Path

What is JAVA_HOME in Ubuntu?

JAVA_HOME is an environment variable in Ubuntu that points to the directory where the Java runtime environment (JRE) or Java development kit (JDK) is installed. It is used by many Java applications and development tools to determine the location of Java.

How do I find my Java installation path in Ubuntu?

You can find your Java installation path by executing update-alternatives --config java in the terminal. This command lists all installed Java versions and their paths.

How do I set JAVA_HOME in Ubuntu?

To set JAVA_HOME, open your terminal and type export JAVA_HOME=/path/to/java. Replace /path/to/java with the actual path to your Java installation.

How do I permanently set the JAVA_HOME variable in Ubuntu?

To permanently set JAVA_HOME, add the export command to your ~/.bashrc or ~/.profile file. For example, add export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64 to the file, and then run source ~/.bashrc or source ~/.profile.

How do I check if JAVA_HOME is set correctly in Ubuntu?

To verify if JAVA_HOME is set correctly, type echo $JAVA_HOME in the terminal. If it displays the path you set, then it is configured correctly.

Why do I need to set JAVA_HOME?

Setting JAVA_HOME in Ubuntu is essential for running Java applications and using development tools. It ensures that these programs can find the Java libraries and binaries they need.

What should I do if I get an error stating JAVA_HOME is not set?

If you receive an error stating JAVA_HOME is not set, ensure you have Java installed and then set the JAVA_HOME variable as described in question 3. If it's already set, check for typos or incorrect paths.

Can I have multiple Java versions and JAVA_HOME paths in Ubuntu?

Yes, you can have multiple Java versions installed. You can switch between them using the update-alternatives --config java command. However, JAVA_HOME can point to only one installation at a time.

Do I need to restart my system after setting JAVA_HOME in Ubuntu?

You usually don't need to restart your system. However, you may need to restart any open terminal sessions or applications that rely on JAVA_HOME.

How do I update JAVA_HOME when I upgrade Java in Ubuntu?

When you upgrade Java, you should update the JAVA_HOME variable to point to the new Java version's installation path. Follow the same steps as setting it for the first time.