Java is an object-oriented programming language developed by Sun Microsystems since 1991 and officially released on May 23, 1995. The new programming language was originally called Oak (James Gosling) and was developed for consumer electronics, but later it was renamed Java and began to be used for writing applets, applications and server software
A peculiarity of Java technology is a flexible security system due to the fact that the program execution is fully controlled by a virtual machine. Any operation that exceeds the program's established authority (e.g., attempting unauthorized access to data or connecting to another computer) causes an immediate interruption.

file

Installing the default JRE/JDK

We will use the version included in the Ubuntu package for installation. By default Ubuntu 18.04 package includes Open JDK (open source version of JRE and JDK).

This package installs version 10 or 11 of OpenJDK.

To install, update the package index:
``.shell
sudo apt update

Then you should check if the installation of Java has been done before:  
``.shell
java -version  

If Java has not been installed, you will see a similar result:
``.shell
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, run the command to install **OpenJDK**:  
``.shell
sudo apt install default-jre  

This command installs the Java Runtime Environment (JRE). It allows you to run almost any Java software.

Check the installation with the following command:
``.shell
java -version


You will see a similar result:  
``.shell
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)  

To compile and run some specific Java-based programs, you may need the Java Development Kit (JDK) in addition to the JRE

To install the JDK we execute the following command which also executes the JRE installation:
``.shell
sudo apt install default-jdk

Next, check the installation of the JDK by checking the version of javac, the Java compiler:  
``.shell
javac -version  

You will see a similar result:
``.shell
root@kvmde67-19464:~# javac -version
javac 11.0.6


#### Installing certain versions of OpenJDK

**OpenJDK 8**
To install OpenJDK 8, run the following command:  
``.shell
sudo apt install openjdk-8-jdk  

To check the installation use the command
``.shell
java -version

You will see a result like this:  
``.shell
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 just the JRE by running the command
``.shell
sudo apt install openjdk-8-jre.

**OpenJDK 10/11**
To install OpenJDK 10/11 use the following command:  
``.shell
sudo apt install openjdk-11-jdk  

To install only JRE, the following command must be used:
.shell sudo apt install openjdk-11-jre


#### Installing Oracle JDK

If you want to install an officially distributed version of Oracle JDK, you need to add a new package repository for the version you want to use.  

To install Java 8, you must first add a package repository for it:  
``.shell
sudo add-apt-repository ppa:webupd8team/java  

When the repository is added, the following message is displayed:
``.shell
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.

Press ENTER to continue  

Next we will update the list of packages:  
``.shell
sudo apt update  

After the update, let's install Java 8:
``.shell
sudo apt install oracle-java8-installer

Your system will download the JDK from Oracle and ask you to accept the license agreement. After accepting the terms of the agreement, the JDK will be installed.  

#### Managing Java

Several versions of Java can be installed on one server. You can set the default version through the command line using the command **update-alternatives**.  
``.shell
sudo update-alternatives --config java  

If you have installed different versions of Java, the result you will see is this:
``.shell
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 to keep the current choice[*], or type selection number

Select the number corresponding to the version of Java to be used by default, or press ENTER to keep the current selection.  

You can also do this for other Java commands such as the compiler (javac):  
``.shell
sudo update-alternatives --config javac  

You will see a similar result:
``.shell
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 to keep the current choice[*], or type selection number


#### Setting the JAVA_HOME environment variable

To set the environment variable, you need to pre-define the Java installation directory. Use the command **update-alternatives**:  
``.shell
sudo update-alternatives --config java  

This command shows each Java installation and installation path:
``.shell
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 to keep the current choice[*], or type selection number

Copy the path of the preferred version's installation directory. Open **/etc/environment** directory with a text editor:  
``.shell
sudo nano /etc/environment  

Add the following line at the end of this file and replace the path copied earlier:

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

Changing the file will set the path JAVA_HOME for all users on the system.

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

Load the file again, to apply the changes to the current session:
``.shell
source /etc/environment

Check that the environment variable is set:  
``.shell
echo $JAVA_HOME  

You will see the path you just set:

root@kvmde67-19464:~# echo $JAVA_HOME  
/usr/lib/jvm/java-11-openjdk-amd64/bin/

Other users will have to run the command source /etc/environment to apply this parameter.


If you have configuration difficulties or have further questions, you can always contact our support team via ticket system.

Updated April 23, 2020