Java is a general-purpose computer programming language that is concurrent, class-based, object-oriented, and specifically designed to have as few implementation dependencies as possible.

In this guide, we will explain how to install and configure Java on an Ubuntu 16.04 server.

To install Java on CentOS 7, you can follow the guide here

Deploying your cloud server
If you have not already registered with Cloudwafer, you should begin by getting signed up. Take a moment to create an account after which you can quickly deploy your cloud servers.

Once you have signed up, log into your Cloudwafer Client Area and deploy your Cloudwafer cloud server.

Updating System Packages
It is recommended that you update the system to the latest packages before beginning any major installations. This is done with the command below:

sudo apt-get update && sudo apt-get upgrade

Install the default JRE/JDK
Ubuntu by default contains the OpenJDK 8 which is the latest and recommended version for installing Java.

Firstly, run the command below to install the Java Runtime Environment (JRE).

sudo apt-get install default-jre

Next, we install the default Java installation called the JDK (Java Development Kit). The JDK is an implementation of either one of the Java Platform, Standard Edition, Java Platform, Enterprise Edition, or Java Platform, Micro Edition platforms released by Oracle Corporation in the form of a binary product aimed at Java developers on Solaris, Linux, macOS or Windows. The JDK contains the JRE so you can install the JDK instead of the JRE.

To install the JDK, issue the following command:

sudo apt-get install default-jdk

Installing the Oracle JDK
To install the official version distributed by Oracle, we will be installing the Oracle JDK. By first adding Oracle's PPA, then updating the package repository.

sudo add-apt-repository ppa:linuxuprising/java

sudo apt-get update

Then, depending on the version, you want to install, execute one of the following commands:

Oracle JDK 11

sudo apt install oracle-java11-installer

Oracle JDK 8

sudo apt-get install oracle-java8-installer

Switching multiple versions of Java
In this guide, we have installed various Java installations on one server. You can configure which version is the default for use in the command line by using update-alternatives, which manages which symbolic links are used for different commands.

sudo update-alternatives --config java

Select the number to use as a default.

Using Environment Variables
Numerous Java applications use the JAVA_HOME or environment variables to determine which java executable to use. To set this environment variable, we will first need to find out where Java is installed. Execute the previous command used in the last section:

sudo update-alternatives --config java

Copy the path from your preferred installation and then open /etc/environment

sudo nano /etc/environment

Add the following line, making sure to replace the highlighted path with your own copied path.

JAVA_HOME="/usr/lib/jvm/java-11-oracle/bin/java"

Save and exit the file, then reload it by.

source /etc/environment

You can now test whether the environment variable has been set by executing the following command:

echo $JAVA_HOME