Sunday, July 22, 2012

OpenJDK on Ubuntu 12.04 (Precise Pangolin)

On a fresh install of Ubuntu 12.04, you might be surprised to find out that there is no Java installed as part of a standard installation! This can be easily verified by executing the following command in a terminal:

java -version

You should see the following response:

  The program 'java' can be found in the following packages:
   * default-jre
   * gcj-4.6-jre-headless
   * openjdk-6-jre-headless
   * gcj-4.5-jre-headless
   * openjdk-7-jre-headless
  Try: sudo apt-get install

Running update-alternatives on Java also results in the message:

  update-alternatives: error: no alternatives for java.

You can also confirm this using the Synaptic Package Manager - which is NOT installed by default on Ubuntu 12.04. Install this package by running:

sudo apt-get install synaptic

The following is a screen-shot of the result of an 'openjdk' search.

Since the purpose of this article is to get Java working on Ubuntu 12.04, we need to get all 'flavors' of Java installed - starting with OpenJDK. Follow the steps outlined below to start installing.

(NOTE: The procedure below is for the 32-bit version of Ubuntu 12.04, but they also applies to the 64-bit version - just replace the 'i386' part,  whenever they occur, with 'x64' for the 64-bit version.)

  1. Start Synaptic and mark the following packages for installation:


    Press the 'Apply' button and wait for the download and install process to finish.

  2. Use the update-alternatives command again to see which OpenJDK is set as the 'default' Java - you should see:

    It's clear from the screen that OpenJDK 6 is the 'default' Java and you could use the 'java -version' command to verify this - the output should show:

    java version "1.6.0_24"
    OpenJDK Runtime Environment (IcedTea6 1.11.3)

    OpenJDK Server VM (build 20.0-b12, mixed mode)

  3. To switch between the OpenJDK versions (6 or 7), you can either use the command line 'update-alternatives' utility or the graphical front-end for the same command - 'galternatives'. To use 'galternatives', you need, of course, to install it first by executing:

    sudo apt-get install galternatives

    Alternatively, you could use the Synaptic Package Manager to install this. Once installed, open the dash (if you are using Unity) or the menu (if you are using gnome-shell) and locate the 'Alternative Configurator' utility. Start it, enter your password (the utility requires root privileges), and you should see the following screen:

    Scroll down the left-hand pane until you see the 'java' entry (see figure below) - note that OpenJDK 6 is set as the default. You can use the galternatives utility to change OpenJDK versions by clicking on the radio-button under the 'Choice' column. You should also remember to change the other Java related items (those that starts with the letter 'j') whenever you change versions.

  4. A faster way to switch between OpenJDK versions at one go, is to use the 'update-java-0.5b' utility. This utility can be downloaded by executing:


    Once the script file has been downloaded, execute:

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

    You should see a dialog box (as shown) pop-up:

    Note that the entry '/usr/lib/jvm/java-1.6.0-openjdk-i386' and '/usr/lib/jvm/java-1.7.0-openjdk-i386' are links to the actual directories 'usr/lib/jvm/java-6-openjdk-i386' and 'usr/lib/jvm/java-7-openjdk-i386' respectively - so it really does not matter which one you select - they mean the same thing. Do not select the 'usr/lib/jvm/java-6-openjdk-common' or the 'usr/lib/jvm/java-7-openjdk-common' entries. First select the 'usr/lib/jvm/java-7-openjdk-i386' entry and click 'OK'. The utility will make all necessary changes for you at one go. To verify run 'galternatives' again and you should see:

    You can verify that the 'update-java' utility has made other changes by selecting the Java related items on the left-hand pane and seeing the result in the right-hand pane.

    Now run the sudo 'update-java-0.5b' utility again - this time select the 'usr/lib/jvm/java-6-openjdk-i386' entry and click 'OK'. Again, the utility will make all necessary changes for you at one go. To verify run 'galternatives' and you should see:

That's it, you can now choose freely and easily between versions of OpenJDK Java. Enjoy!

No comments:

Post a Comment