How to Install Java with Apt-Get on Ubuntu 16.04


Java programming language was originally developed by Sun Microsystems which was initiated by James Gosling and released in 1995 as core a component of Sun Microsystems’ Java platform (Java 1.0 [J2SE]).

The latest release of the Java Standard Edition is Java SE 8. With the advancement of Java and its widespread popularity, multiple configurations were built to suit various types of platforms. Ex − J2EE for Enterprise Applications, J2ME for Mobile Applications.

The new J2 versions was renamed as Java SE, Java EE and Java ME respectively. Java is guaranteed to be a Write Once, Run Anywhere.This article explains about ‘How to install Java with Apt-get on Ubuntu’

Installing the Default JRE/JDK

To install the Java Runtime Environment (JRE), use the following command –

$ sudo apt-get install default-jre

The sample output should be like this –

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
   apport-hooks-elementary contractor javascript-common libgda-5.0-4
   libgda-5.0-common libgranite-common libgranite3 libgsignon-glib1
   libindicate5 libjs-jquery libnoise-core0 libtagc0 mysql-server-5.7
   mysql-server-core-5.7
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
   ca-certificates-java default-jre-headless fonts-dejavu-extra java-common
   libbonobo2-0 libbonobo2-common libgif7 libgnome-2-0 libgnome2-common
   libgnomevfs2-0 libgnomevfs2-common liborbit-2-0 openjdk-8-jre
openjdk-8-jre-headless
Suggested packages:
   default-java-plugin libbonobo2-bin desktop-base libgnomevfs2-bin
   libgnomevfs2-extra gamin | fam gnome-mime-data icedtea-8-plugin
   openjdk-8-jre-jamvm fonts-ipafont-gothic fonts-ipafont-mincho
   ttf-wqy-microhei | ttf-wqy-zenhei fonts-indic
The following NEW packages will be installed:
   ca-certificates-java default-jre default-jre-headless fonts-dejavu-extra
   java-common libbonobo2-0 libbonobo2-common libgif7 libgnome-2-0
...........................................................................

There is another default Java installation called the JDK (Java Development Kit). The JDK is usually needed if you are going to compile Java programs or if the software that will use Java specifically requires it. To install JDK, use the following command –

$ sudo apt-get install default-jdk

The sample output should be like this –

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
   apport-hooks-elementary contractor javascript-common libgda-5.0-4
   libgda-5.0-common libgranite-common libgranite3 libgsignon-glib1
   libindicate5 libjs-jquery libnoise-core0 libtagc0 mysql-server-5.7
   mysql-server-core-5.7
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
   default-jdk-headless libice-dev libpthread-stubs0-dev libsm-dev libx11-dev
   libx11-doc libxau-dev libxcb1-dev libxdmcp-dev libxt-dev openjdk-8-jdk
   openjdk-8-jdk-headless x11proto-core-dev x11proto-input-dev x11proto-kb-dev
   xorg-sgml-doctools xtrans-dev
Suggested packages:
   libice-doc libsm-doc libxcb-doc libxt-doc openjdk-8-demo openjdk-8-source
   visualvm
The following NEW packages will be installed:
   default-jdk default-jdk-headless libice-dev libpthread-stubs0-dev libsm-dev
   libx11-dev libx11-doc libxau-dev libxcb1-dev libxdmcp-dev libxt-dev
   openjdk-8-jdk openjdk-8-jdk-headless x11proto-core-dev x11proto-input-dev
   x11proto-kb-dev xorg-sgml-doctools xtrans-dev
0 upgraded, 18 newly installed, 0 to remove and 168 not upgraded.
Need to get 11.9 MB of archives.
.....................................................................................

Installing the Oracle JDK

To install the oracle JDK, use the following command –

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

The sample output should be like this –

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

More info (and Ubuntu installation instructions):
- for Oracle Java 7: http://www.webupd8.org/2012/01/install-oracle-java-jdk-7-in-ubuntu-via.html
- for Oracle Java 8: http://www.webupd8.org/2012/09/install-oracle-java-8-in-ubuntu-via-ppa.html

Debian installation instructions:
- Oracle Java 7: http://www.webupd8.org/2012/06/how-to-install-oracle-java-7-in-debian.html
- Oracle Java 8: http://www.webupd8.org/2014/03/how-to-install-oracle-java-8-in-debian.html

Important!!! For now, you should continue to use Java 8 because Oracle Java 9 is available as an early access release (it should be released in 2016)! You should only use Oracle Java 9 if you explicitly need it, because it may contain bugs and it might not include the latest security patches! Also, some Java options were removed in JDK9, so you may encounter issues with various Java apps. More information and installation instructions (Ubuntu / Linux Mint / Debian): http://www.webupd8.org/2015/02/install-oracle-java-9-in-ubuntu-linux.html
   More info: https://launchpad.net/~webupd8team/+archive/ubuntu/java
Press [ENTER] to continue or ctrl-c to cancel adding it

gpg: keyring `/tmp/tmpa5dj4h41/secring.gpg' created
gpg: keyring `/tmp/tmpa5dj4h41/pubring.gpg' created
gpg: requesting key EEA14886 from hkp server keyserver.ubuntu.com
..........................................................................................

Now update the packages list using the following command –

$ sudo apt-get update

Managing Java

There may be multiple Java installations on one server. You could configure a specific model which can be used as a default to be used within the command line through the use of update-options as shown below-

$ sudo update-alternatives --config java

The sample output should be like this –

There are 5 choices for the alternative 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 

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

you can choose as per your knowledge.

Setting the JAVA_HOME Environment Variable

To set this environment variable, we will first need to find out where Java is installed. You can do this by executing the following command –

$ sudo update-alternatives --config java

Copy the path from your preferred installation and then open /etc/environment file as shown below –

$ sudo nano /etc/environment

At the end of the file add the following lines –

JAVA_HOME="YOUR_JAVA_PATH"

For example, it should be as shown below –

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

Save and exit the file, and reload it as shown below-

$ source /etc/environment

you may now test whether or not the environment variable has been set by using executing the following command-

$ echo $JAVA_HOME

After this article, you will be able to understand – How To Install Java with Apt-Get on Ubuntu 16.04. In our next articles, we will come up with more Linux based tricks and tips. Keep reading!

Updated on: 22-Jan-2020

833 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements