Installation of Java in Ubuntu 18.04

  • JRE/JDK default installation
  • Installation of specified versions of OpenJDK
  • Oracle JDK installation
  • Java control
  • Setting a JAVA_HOME environment variable

Java is an object-oriented programming language that the Sun Microsystems company has been designing since 1991. It was officially launched on May 23, 1995. The new programming language was originally called Oak (James Gosling) and was designed for electronics used at home. Yet, it was later renamed Java and people started using it for writing applets, applications, and server software.

A special feature of Java technology is a flexible security system. The reason for that is that program performance is totally under the control of a virtual machine. Any operations beyond the set powers of the program cause immediate interruption. For example, they can include an attempt of unauthorized access to data or connection to another PC.

JRE/JDK Default Installation

The installation requires a version contained in the Ubuntu installation package. The default Ubuntu 18.04 package includes Open JDK. It is an open-source JRE and JDK version.

The package is available to install the OpenJDK version number 10 or 11.

You should update the package indexes for the installation:

sudo apt update 

Then you should check if Java installation was conducted earlier:

java -version 

If there was no Java installation, you will get a result like this:

root@kvmde67-19464:~# java -version 

 

Command 'java' not found, but can be installed with: 

 

apt install default-jre 

apt install openjdk-11-jre-headless 

apt install openjdk-8-jre-headless 

Next, launch a command for the installation of OpenJDK:

sudo apt install default-jre 

The command installs the Java Runtime Environment (JRE). It allows almost any software start.

Check the installation using the following command:

java -version 

You will see a result like this:

root@kvmde67-19464:~# java -version 

openjdk version "11.0.6" 2020-01-14 

OpenJDK Runtime Environment (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1) 

OpenJDK 64-Bit Server VM (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1, mixed mode, sharing) 

For the compilation and start of some specific programs based on Java together with JRE, you may need the Java Development Kit (JDK).

To install JDK, you need to apply the following command which also installs JRE:

sudo apt install default-jdk 

Next, check the JDK installation. You can do this by checking the javac version of the Java compiler:

javac -version 

You will see the results as follows:

root@kvmde67-19464:~# javac -version 

javac 11.0.6 

Installation of Specified Versions of OpenJDK

OpenJDK 8

For OpenJDK 8 installation, you should apply the following command:

sudo apt install openjdk-8-jdk 

To check the installation, apply the command:

java -version 

You will see the result as follows:

root@kvmde67-19464:~# java -version 

openjdk version "1.8.0_162" 

OpenJDK Runtime Environment (build 1.8.0_162+10-post-Ubuntu-1ubuntu118.04.1) 

OpenJDK 64-Bit Server VM (build 1.8.0_162+10-post-Ubuntu-1ubuntu118.04.1, mixed mode, sharing) 

You can also install only JRE. To do this, apply the command as follows:

sudo apt install openjdk-8-jre. 

OpenJDK 10/11

For OpenJDK 10/11 installation, use the following command:

sudo apt install openjdk-11-jdk 

To install only JRE, use the following command:

sudo apt install openjdk-11-jre 

Oracle JDK Installation

If you want to install the official version of Oracle JDK, you should add new storage for your used version packages.

To install Java 8, you should add the storage of packages to it first:

sudo add-apt-repository ppa:webupd8team/java 

When you add the storage, you get a message as follows:

Oracle Java (JDK) Installer (automatically downloads and installs Oracle JDK8). There are no actual Java files in this PPA. 

 

Important -> Why Oracle Java 7 And 6 Installers No Longer Work: http://www.webupd8.org/2017/06/why-oracle-java-7-and-6-installers-no.html 

 

Update: Oracle Java 9 has reached end of life: http://www.oracle.com/technetwork/java/javase/downloads/jdk9-downloads-3848520.html 

 

The PPA supports Ubuntu 18.10, 18.04, 16.04, 14.04 and 12.04. 

 

More info (and Ubuntu installation instructions): 

- http://www.webupd8.org/2012/09/install-oracle-java-8-in-ubuntu-via-ppa.html

 

Debian installation instructions: 

- Oracle Java 8: http://www.webupd8.org/2014/03/how-to-install-oracle-java-8-in-debian.html

 More info: https://launchpad.net/~webupd8team/+archive/ubuntu/java

Press [ENTER] to continue or Ctrl-c to cancel adding it. 

To continue the process, press ENTER.

Next, update the list of packages:

sudo apt update 

After the update, start Java 8 installation process:

sudo apt install oracle-java8-installer 

Your system will download JDK from Oracle and ask you to accept the license agreement. After you accept the license agreement terms, the system will start the JDK installation.

Java Control

One server can contain several Java versions. To set a default version, you can use your command line using the update-alternatives command.

sudo update-alternatives --config java 

If you have several Java versions, you will see the result as follows:

root@kvmde67-19464:~# sudo update-alternatives --config java 

There are 2 choices for the alternative java (providing /usr/bin/java). 

 

  Selection    Path                                            Priority   Status

------------------------------------------------------------

* 0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java  1111      auto mode

  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java  1111      manual mode

  2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

 

Press <enter> to keep the current choice[*], or type selection number: 

Choose a figure corresponding to the default Java version, or press ENTER to save your current settings.

You can also do this for other Java commands like the javac compiler:

sudo update-alternatives --config javac 

You will see the result as follows:

root@kvmde67-19464:~# sudo update-alternatives --config javac 

There are 2 choices for the alternative javac (providing /usr/bin/javac). 

 

  Selection    Path                                      Priority   Status

------------------------------------------------------------

* 0            /usr/lib/jvm/java-11-openjdk-amd64/bin/javac   1111      auto mode

  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/javac   1111      manual mode

  2            /usr/lib/jvm/java-8-openjdk-amd64/bin/javac 1081      manual mode

 

Press <enter> to keep the current choice[*], or type selection number: 

Setting a JAVA_HOME Environment Variable

To set an environment variable, you need to define the installation catalog first. To do this, apply the update-alternatives command:

sudo update-alternatives --config java 

The command shows each Java installation and the installation route:

root@kvmde67-19464:~# sudo update-alternatives --config java 

There are 2 choices for the alternative java (providing /usr/bin/java). 

 

  Selection    Path                                            Priority   Status

------------------------------------------------------------

* 0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java  1111      auto mode

  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java  1111      manual mode

  2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

 

Press <enter> to keep the current choice[*], or type selection number: 

Copy the installation catalog directory of the version you are going to install. Then, open the catalog /etc/environment in a text editor:

sudo nano /etc/environment 

Add the following line at the end of the file and replace the directory with the one copied before:

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

After the file is changed, the system will set the JAVA_HOME directory for all users of the system.

Save the changes and close the file (CTRL + X, Y, Enter).

Download the file again to apply the changes to the current session:

source /etc/environment 

Check the set environment variable:

echo $JAVA_HOME 

You will see the directory you have just created:

root@kvmde67-19464:~# echo $JAVA_HOME 

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

To apply the setting, other users will need to use the command source /etc/environment.

Blog