How to install Oracle’s Java on Ubuntu the easy way

Tweet about this on Twitter4Share on Facebook8Share on Google+2Share on LinkedIn0Share on Reddit0Email this to someone

Developers on Ubuntu are accustomed to seeing at least two flavors of Java.  OpenJDK is an open-source build of the Java runtime and compiler.  The Oracle JDK is built upon this, but adds some closed-source components.

Ubuntu and Java logosIn theory, OpenJDK is the official reference implementation of Java 7, and should be perfectly fine for all your needs.  In practice, it is still known for having issues with common app servers and frameworks.  OpenJDK is often fine for end-users, but most Java developers on Ubuntu end up installing the Oracle JDK.

Up until 2011, Canonical distributed the Sun/Oracle JDK in the default Ubuntu apt repository.  However, Oracle changed the licensing terms of Java, and this was no longer possible.  With the official JDK gone from the Ubuntu repository, developers had to download it directly from Oracle.  Installation and configuration became a torturous 18-step process.

There is an easier way, which takes you back to the familiar apt-get, and away from wrestling with sudo update-alternatives.  There is a project on GitHub called OAB-Java, which basically consists of a single shell script.  With one command, this script will:

  1. Download the desired JDK (Java 6 or 7) from Oracle
  2. Assemble it into a .deb package
  3. Create a local apt repository, containing this package
  4. Add it as an available repository, so that it works with apt-get, Synaptic, or Update Manager

Installing and running this script is as simple as:

wget https://github.com/flexiondotorg/oab-java6/raw/0.2.7/oab-java.sh -O oab-java.sh
chmod +x oab-java.sh
sudo ./oab-java.sh -7

Notes:

  • The first line refers to version 0.2.7 of the script.  There may be a newer version available when you read this, so you should check the GitHub page for the lastest wget URL.
  • The last line is passed a -7 parameter, signifying that we want Java 7 instead of the default Java 6.
    • This is the only thing I don’t like about this script.  Java 6 is very outdated, and nearing the end of its support cycle.  You almost certainly want Java 7, but it’s easy to get the wrong version by forgetting to pass this optional -7 parameter.

After the script has run, the Oracle JDK will be available in your list of apt sources.  It can be installed through Synaptic, or in a console session like this:

apt-get install oracle-java7-jdk

That’s it!  You don’t have to force the Oracle JDK into the available list with update-alternatives, or reload your system PATH.  You are simply good to go, just like the days back when Canonical distributed the Sun JDK themselves.