How to Install OpenJDK (JAVA) in Ubuntu

Installing java in Linux/UNIX based system can be a bit tricky sometimes because it requires the user to set an environment variable called JAVA_HOME and finding the right version of java can be sometimes overwhelming. To stay in the open-source realm, we will use openjdk-8-jdk. First, we need to make sure that currently, there is no java installation.

java -version

This should give back a response that shows that java is not installed and a process to install it. It should produce a result something like below

The program 'java' can be found in the following packages:
* default-jre
* gcj-5-jre-headless
* openjdk-8-jre-headless
* gcj-4.8-jre-headless
* gcj-4.9-jre-headless
* openjdk-9-jre-headless
Try: sudo apt install <selected package>

Once we are sure that there is no current java installation. We can proceed with installing java

sudo apt-get -y install openjdk-8-jdk

The above command should install the openjdk-8. Check if java has been installed correctly using the following command

java -version

This time, the above command should return something like below

openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-2ubuntu0.16.04.1-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)

Once java is installed, we need to set JAVA_HOME environment variable. This environment variable is required by many services and it is not set by java itself.

Handle JAVA_HOME

Adding java executable directory to the system environment variable can be done in a few different ways. We will try a technique that will persist over reboots.

First, we need to find out the absolute location path of java executable in the system. To do so, please use the following command

sudo update-alternatives --config java

This should respond with the java path, priority and status. The response should look something like below

There is only one alternative in link group java (providing /usr/bin/java):
/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
Nothing to configure.

This gives us the location of the java-8-openjdk installation. If we take a look into /usr/lib/jvm/java-8-openjdk-amd64/ directory, we will find a bin directory that houses the java (it could be sym-link) executable. So this gives us the value for JAVA_HOME. If we sum it up JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/bin/ (could be different based on your system)

To add this variable to the system environment, use the commands that are listed below

sudo nano /etc/environment

At the end of the file, add the following line

JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/bin/"

Now, save the file and exit the editor. (Ctrl+oENTERCtrl+x)

To use the newly set variable without a reboot, please use

source /etc/environment

Verify that the environment variable is set

echo $JAVA_HOME

This should print out the JAVA_HOME path that was just set

/usr/lib/jvm/java-8-openjdk-amd64/bin/

If everything runs without any error then openjdk and JAVA_HOME are properly configured in the system.

Enjoy!