How To Setup Java_Home Path In Ubuntu


If you are using any integrated development environment (IDE) such as Netbeans, Eclipse, etc., for java based programming if your IDE automatically installs and sets the required variables for you to do java programming, you will never hear from your system

But more often than not this doesn’t happen so you will encounter issues where your system will throw errors such as ‘java_home environment variable is not set.’ This is because IDE requires Java Development Kit(JDK) to be pre-installed on the computer.

Here’s a simple solution on how we can go setting ‘java_home environment variable’, this solution will primarily target Ubuntu however the solution should also work with other distributions.

Let’s start off with installing OpenJDK on Ubuntu

Step 1. In the bottom right side press show applications

Screenshot 2022 10 13 At 9.09.34 AM
Press Show Applications

Step 2. Press the search box at the top of the launcher

Screenshot 2022 10 13 At 9.09.49 AM
Click on the Search box at the top

Step 3. Type terminal in the search box

Screenshot 2022 10 13 At 9.10.04 AM
Search for the terminal in the search box

Step 4. Open Terminal

Screenshot 2022 10 13 At 9.10.18 AM 1
Open terminal

Alternatively, you can also just press CTRL+ALT+T to open up the terminal.

Step 5. We need to check what versions of OpenJDK are compatible with our system.

To check what all versions of OpenJDK are compatible with my system type the following command

apt-cache search openjdk-
Screenshot 2022 10 13 At 9.14.06 AM
Checking compatible version of openjdk
Screenshot 2022 10 13 At 9.12.54 AM
Results of apt-cache search openjdk-

As you can see, our system supports OpenJDK 8, OpenJDK 11, OpenJDK 17, and OpenJDK 18.

Since I like to be on the cutting edge of technology, I’m going to install OpenJDK 18.

Image 3
Thank you, this cutting edge I will take.

Step 6. We will install OpenJDK 18, using the following command

sudo apt install openjdk-18-jdk
Screenshot 2022 10 13 At 9.15.49 AM
Installing OpenJDK 18
Screenshot 2022 10 13 At 9.17.24 AM
OpenJDK 18 installation

Step 7. Let’s check the version of OpenJDK that has been installed

We do this by entering the following command in the terminal,

javac --version
Screenshot 2022 10 13 At 9.58.22 AM
Checking which version of OpenJDK has been installed

A note on the versions compatible with your system

Since I’m working on an arm-based system the selection of the versions that can be installed in my system is limited.

If you are running an x86 or x64 system this may not be the case as these systems are widely popular. So your system may support more versions of OpenJDK.

Following are the commands to install specific versions,

If you want to install the default version of JDK enter the following in the terminal

sudo apt install default-jdk

For installing OpenJDK 8

sudo apt install openjdk-8-jdk

For installing OpenJDK 11

sudo apt install openjdk-11-jdk

For installing OpenJDK 14

sudo apt install openjdk-14-jdk

For installing OpenJDK 16

sudo apt install openjdk-16-jdk

For installing OpenJDK 17

sudo apt install openjdk-17-jdk

For installing OpenJDK 18

sudo apt install openjdk-18-jdk

Locate where OpenJDK is installed in the system

Usually, programs get installed in a default location specified by the developer. However, sometimes the default location changes, or the user decides to install in a different location. It is best to check where OpenJDK is installed by running the following command:

which java
Screenshot 2022 10 13 At 11.29.58 AM
The location of this version of OpenJDK is located at /usr/bin/java

Checking the value of JAVA_HOME directory

The echo command is used to print the value of a given variable. In this case, the $JAVA_HOME variable contains the path to the Java installation directory.


Screenshot 2022 10 13 At 11.31.56 AM
For the above image by default, the JAVA_HOME directory has no value set

This however is a symbolic link, we need to get the actual location of the java_home directory. So we need to execute the following command in the terminal.

readlink -f `which javac` | sed "s:/bin/javac::"
Screenshot 2022 10 13 At 8.02.53 PM
Running the readlink command

The above code is a command that will return the full path of the javac command. The command uses the readlink command to return the full path of the javac command and uses the sed command to remove the /bin/javac portion of the path.

Setting the JAVA_HOME path variable

Now that we have the JAVA_HOME directory location let us set that up using,

export JAVA_HOME=/usr/lib/jvm/java-18-openjdk-arm64

A note on the above location since I’m using an arm64-based system the location of my JAVA_HOME is different but the procedure for setting the variable and the discovery of it will remain the same.

Screenshot 2022 10 14 At 11.00.25 AM
Setting path variable

Now, let us check if the variables have been set successfully using echo command.

Screenshot 2022 10 14 At 11.04.39 AM
The variable has been successfully set!

There is an issue with this though once we are done with the current session, the path will change back to being nothing just like how it was previously.

To make the changes permanent we need to make a few more changes.

Assuming that you are using bash as your primary shell as it is the default shell shipped with Ubuntu.

We will be setting this variable to permanently store the JAVA_HOME path.

First, let’s take a backup of the bashrc configuration file,

cp ~/.bashrc ~/.bashrc.bak
Screenshot 2022 10 14 At 12.51.05 PM
backing up bashrc

Finally, we set up the JAVA_HOME permanently using the below command,

echo "export JAVA_HOME=/usr/lib/jvm/java-18-openjdk-arm64" >> ~/.bashrc
Screenshot 2022 10 14 At 12.57.38 PM
Permanently setting up the environment variable

Let’s check whether the changes we made took place using the tail command,

tail -5 ~/.bashrc 
Screenshot 2022 10 14 At 1.04.33 PM
Using tail command to check if our changes took place.

The tail command is used to print the last few lines of a file. The -5 option tells the tail command to print the last 5 lines of the file. The ~/.bashrc file is a configuration file for the bash shell. It contains a variety of settings, including the setting of environment variables.

That’s it we successfully set up the Java_Home Path In Ubuntu Permanently.