Install Java with Apt-Get on Ubuntu

In this article we will discuss how to install Java on Ubuntu Cloud server using Apt-Get.


Installing the Default JRE/JDK

The most simple and easiest way for installing Java is utilizing the version packed with Ubuntu.  Mainly this will install OpenJDK 8, the most recent and highly recommended version.

First and foremost, update the package index using:

$ sudo apt-get update

Then, install Java. Especially, this will introduce the JRE (Java Runtime Environment)).

$ sudo apt-get install default-jre

There is another default Java installation known as JDK (Java Development Kit). The JDK is typically just required in the event that you will compile Java programs or if the product that will utilize Java particularly requires it.

You can easily Install the JDK using below mentioned command:

$ sudo apt-get install default-jdk

Installing the Oracle JDK

In case you need to install the Oracle JDK, which is the official version disseminated by Oracle, you should take after a couple of more steps.

First of all you need to add Oracle's PPA, after that update your package repository.

$ sudo add-apt-repository ppa:webupd8team/java

$ sudo apt-get update


AFter that based on the version you wish to install, execute any of the following commands:

Oracle JDK 8

This is the most recent stable version of Java at time of composing this tutorial, and the prescribed version to install. You can do as this utilizing the accompanying command:

$ sudo apt-get install oracle-java8-installer


Oracle JDK 9

To install Oracle JDK 9, use the given below command:

$ sudo apt-get install oracle-java9-installer


Managing Java

There can be numerous Java based applications on one single server. You can easily configure which version is the default for use in the command line by utilizing update-alternatives, which handles which representative links are utilized for various commands.

$ sudo update-alternatives --config java


The result will look similar like the following. In this situation, this is what the result will look similar with all Java versions discussed above installed.


Mainly there are 5 different alternative for the java (providing /usr/bin/java).

 Selection    Path                                   Priority Status
* 0            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081 auto mode
 1           /usr/lib/jvm/java-6-oracle/jre/bin/java          1 manual mode
 2           /usr/lib/jvm/java-7-oracle/jre/bin/java          2 manual mode
 3           /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081 manual mode
 4           /usr/lib/jvm/java-8-oracle/jre/bin/java          3 manual mode
 5           /usr/lib/jvm/java-9-oracle/bin/java              4 manual mode


Hit <enter> to keep the current option[*], or enter selection number:

Now you would be able to pick the number to use as a default. This should likewise be possible for other Java commands, for example, the compiler (javac), the documentation generator (javadoc), the JAR signing instrument (jarsigner), and that's just the beginning. You can utilize the following command, putting in the command you need to customize.

$ sudo update-alternatives --config command


JAVA_HOME Environment Variable Setting

Numerous programs, for example, Java servers, utilize the JAVA_HOME environment variable to decide the Java installation area. To put this environment variable, we will first of all have to discover where Java is installed. You can do this by implementing an indistinguishable command from in the past section:

$ sudo update-alternatives --config java


Just copy the path from your favored installation and afterward open/etc/environment  utilizing nano or your most loved text editor.

$ sudo nano /etc/environment


At the finish of this file, include the following line, making a point to supplant the featured way with your own copied path.


$ JAVA_HOME="/usr/lib/jvm/java-8-oracle"


Now just save and exit the file, and refresh it.

$ source /etc/environment


You would now be able to test whether the environment variable has been set by executing the accompanying command:

$ echo $JAVA_HOME


This will give back the path you just set.