Thursday, July 26, 2012

Oracle Java on Ubuntu 12.04 (32 & 64 Bits)

Ubuntu Linux no longer distributes Oracle's (formerly Sun) Java JDK and JRE. Ubuntu (and Linux Mint) officially supports OpenJDK implementation of Java which is the base for Oracle's own implementation. However, if you want to use Oracle's Java 6 and/or 7, you can download them from Oracle's website. This article shows you how to install and configure both Java 6 and 7 alongside the OpenJDK versions 6 and 7 previously installed. I will also show you how to switch between the versions of Java installed.

Note that since Oracle Java is not officially part of Ubuntu, we cannot use the Synaptic Package Manager or Ubuntu Software Center to search for and install the necessary packages (i.e., it is not part of the repository). We have to resort to using the command line to carry out installation. This also means that notifications on new versions (or updates) are to be done manually by visiting Oracle's website once a while to check for new updates.

To download Oracle's Java, visit their website at:

  http://www.oracle.com/technetwork/java/javase/downloads/index.html

Navigate the site and download the following files for 32-bit Ubuntu:

  jdk-6u33-linux-i586.bin
  jdk-6u30-apidocs.zip
  jdk-6u33-linux-i586-demos.tar.gz
  jdk-7u5-linux-i586.tar.gz
  jdk-7u4-apidocs.zip
  jdk-7u5-linux-i586-demos.tar.gz

For 64-bit Ubuntu, the following files need to be downloaded:

  jdk-6u33-linux-x64.bin
  jdk-6u30-apidocs.zip
  jdk-6u33-linux-x64-demos.tar.gz
  jdk-7u5-linux-x64.tar.gz
  jdk-7u4-apidocs.zip
  jdk-7u5-linux-x64-demos.tar.gz

These are the latest versions (as of July 2012) available on the website. Note that the Java 6 JDK files are supplied in bin format while the Java 7 JDK files are supplied in tar.gz format. This means that we have to use different methods for installing them.

To install both the JDK 6 and JDK 7, follow the steps below. Note that the steps are for the 32-bit version of Mint. Replace the 'i586' portion of the filename with 'x64' for the 64-bit version.

  1. Open a terminal and change to the 'Downloads' directory:

    cd Downloads/

  2. Make the JDK 6 bin file executable by executing:

    chmod a+x jdk-6u33-linux-i586.bin

  3. Execute the JDK 6 bin file:

    ./jdk-6u33-linux-i586.bin

    This will create a 'jdk1.6.0_33' directory and all the files will be unzipped there.

  4. Unzip the JDK 7 file by executing:

    tar -xvf jdk-7u5-linux-i586.tar.gz

    This will create a 'jdk1.7.0_05' directory and all the files will be unzipped there.

  5. Unzip the JDK 6 and JDK 7 docs files by executing:

    unzip jdk-6u30-apidocs.zip -d jdk1.6.0_33/
    unzip jdk-7u4-apidocs.zip -d jdk1.7.0_05/

  6. Unzip the JDK 6 and JDK 7 demo & sample files:

    tar -xvf jdk-6u33-linux-i586-demos.tar.gz
    tar -xvf jdk-7u5-linux-i586-demos.tar.gz

  7. Now move the JDK 6 and JDK 7 directories to /usr/lib/jvm - it's 'proper' place.

    sudo mv jdk1.6.0_33 /usr/lib/jvm
    sudo mv jdk1.7.0_05 /usr/lib/jvm

  8. Now we need to tell the system that we have Oracle's JDK 6 and JDK 7 as alternatives to the OpenJDK. Of course, we can do this manually by using the 'update-alternatives --install xxx' commands for each item, but a far simpler method is to run a ready-made script to do this. The script can be downloaded by executing:

    wget http://webupd8.googlecode.com/files/update-java-0.5b

    NOTE: If you cannot download it using wget as shown above (ERROR 404: not found), go to this website and click on the file to download it:

    http://code.google.com/p/webupd8/downloads/detail?name=update-java-0.5b


  9. Once the script file has been downloaded, execute:

    chmod +x update-java-0.5b
    sudo ./update-java-0.5b

    A dialog box will pop-up showing all the possible candidates - first, select JDK 6 and press 'OK'. The script will run, and make all the necessary changes for you. Now run the script again - this time select JDK 7 and press 'OK'. That's it - you now have OpenJDK versions 6 and 7 as well as Oracle JDK6 and JDK7 as alternatives. To check which Java is set as 'default', run the 'galternatives' utility. The display will look like the figure shown below.

1 comment: