How to Install Java on Ubuntu Linux

Introduction

How to install Java on Ubuntu Linux? Although, this topic is a quite self explanatory to an experienced Linux system administrator it still creates lots of confusion for beginners in terms what version of Java I need, how do I install it or how to change my system settings between multiple different types of Java versions. The aim of this short article is to shed some light on this topic as we will show how to install Java JDK for both Oracle and as well as OpenJDK.

What is Java

In short, Java is an object-oriented programming language. The current owner of the official implementation of the Java SE ( Standard Edition ) platform is Oracle Corporation. The free and open source implementation of the Java Platform SE is called OpenJDK and OpenJRE. There is also another Java version maintained by IBM. IBM provides also both JDK and JRE. Currently only OpenJDK and OpenJRE Java versions are available via standard Ubuntu repository.

What Java version should I install?

This really depends on why you need Java to be installed at the first place. Some software requires to have Oracle’s Java to be installed in order to run properly. If the software you are trying to run does not explicitly say that it needs Oracle’s Java implementation it is always safe and recommended to start with OpenJDK version. If problems occur simply reconfigure you system to use Oracle’s Java SE as a default.

Java JDK vs Java JRE

The difference between JDK ( Java Development Kit ) and JRE ( Java Runtime Environment ) is simply only in a way it it used. If you wish to develop applications and applets you will need to install Java JDK. If you only wish to run Java Applications and applets you only need JRE version.

Java OpenJDK Ubuntu Install

Installation of Java environment on Ubuntu is not performed by default. First make sure that your repositories are working properly by:

$ apt-cache search openjdk

This should produce list of Java related packages such as:

....
penjdk-7-doc - OpenJDK Development Kit (JDK) documentation
openjdk-7-jdk - OpenJDK Development Kit (JDK)
openjdk-7-jre - OpenJDK Java runtime, using Hotspot JIT
....

Once confirmed, perform Java installation with:

$ sudo apt-get install openjdk-7-jdk

The above command will download and install OpenJDK Development Kit version 7 including its all prerequisites. If you require older version of Java to be installed you can do so by installing version 6 with:

$ sudo apt-get install openjdk-6-jdk

Version Check

Once installed you can confirm currently installed Java version by:

$ java -version
java version "1.7.0_09"
OpenJDK Runtime Environment (IcedTea7 2.3.3) (7u9-2.3.3-0ubuntu1~12.04.1)
OpenJDK 64-Bit Server VM (build 23.2-b09, mixed mode)

Oracle Java Ubuntu install

In case that you really need to use Oracle’s proprietary Java SE you can install it on top of your OpenJDK installation and then simply reconfigure your system to use Oracle’s Java as default. The Oracle Java JDK is no longer available via standard Ubuntu repository. However, we can use WebUpd8 Oracle Java PPA. First, we need to update apt sources file:

$ su -
# echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu precise main" \
 >> /etc/apt/sources.list
# echo "deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu precise main"  \
>> /etc/apt/sources.list
# apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EEA14886

Once source files are in place we can use the apt command to Install Java:

# apt-get update
# apt-get install oracle-java7-installer

The above commands will Download, compile and install Java 7 on your system.

Version Check

The same as with OpenJDK, be sure to test the Java version after the Java installation is finished.

$ java -version
java version "1.7.0_11"
Java(TM) SE Runtime Environment (build 1.7.0_11-b21)
Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode)

Managing Multiple Java Installations

If you have installed multiple Java versions such as Open JDK or Oracle SE on the same Ubuntu Linux system you can switch between all Java installation using the update-alternatives command.

$ sudo update-alternatives --config java
[sudo] password for lubos: 
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-7-oracle/jre/bin/java          1052      auto mode
  1            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1051      manual mode
  2            /usr/lib/jvm/java-7-oracle/jre/bin/java          1052      manual mode

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

At this point all you need to do is to, simply, enter an integer number corresponding with the version of Java as shown on the update-alternatives command’s output.

Conclusion

This article showed basic Java Installation on Ubuntu Linux. We did not cover IBM’s Java installation. IBM’s Java version comes for many platforms starting with IBM’s PowerPC. IBM’s Java is used by SAP applications such as SAP Hana and etc. Our next article will describe on how to install Oracle Java JDK 7 from source code or RPM package on Ubuntu Linux.