How to Install Java Runtime In Ubuntu 12.04

java-ubuntu-mainIf you have installed Ubuntu from scratch, most probably the java package is not installed by default. The java package is necessary to run any java-based app, including running java applets in the browser. While you may not use it it all the time, it is still handy to install it. When you need it, it is there ready to be used.

Sun JDK vs OpenJDK

In the past, Sun JDK was the default java package in Ubuntu. After Sun Microsystem was acquired by Oracle in 2010, Ubuntu (and various distros) switched the java package to the fully open-source OpenJDK as the SUN JDK no longer contain codes that are fully open-sourced.

OpenJDK is community-built and is (almost) 100% similar to the SUN JDK. You can easily install OpenJDK in Ubuntu via the Ubuntu Software Center, by clicking here or running the command:

sudo apt-get install openjdk-7-jre

Personally, other than some minor font rendering issue with OpenJDK, I have not experience any major problem with OpenJDK. That could also because I don’t use many java-based apps in the first place. However, if you have a specific java-based app that don’t run well in OpenJDK, you can switch to the SUN JDK instead. Here is how you do it:

The hard way

This method requires you to install SUN JDK 7 manually. It is a good way for you to learn the trick.

1. Download the SUN JDK 7 here to your home folder.

2. Extract the tar file.

3. Move the extracted file to the “/usr/lib/jvm/” folder:

sudo mv jdk1.7.0_04 /usr/lib/jvm/

4. Install SUN JDK 7 in your system:

sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.7.0_04/bin/javac 1
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.7.0_04/bin/java 1

5. Set the environment variables:

sudo update-alternatives --config javac
sudo update-alternatives --config java

You will see an option like the screenshot below:

java-update-alternative

Enter the option that corresponds to the SUN JDK package. Press Enter.

That’s it.

To test your java:

java -version

You should see something like this:

java-version

The easy way

If you don’t want to manually install the SUN JDK, you can do it via a PPA. Since there is no official PPA for SUN JDK, you will have to grab a custom PPA that comes with the java package. Note that custom PPAs might add extra software sources to the repository, and cause your system to be bloated or even have conflicts with other apps. So bear in mind the risk involved.

There are several PPAs out there that come with SUN JDK, one that you can use is “webupd8team/java

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer

This should install SUN JDK 7 in your system.

Run Java applet in Browsers (Firefox, Chrome etc)

Even after you have installed the java package, you will find that the java applet in your browser won’t run. If you are using the OpenJDK, here is another package that you need to install:

sudo apt-get install icedtea-7-plugin

For Oracle java, run the commands:

mkdir ~/.mozilla/plugins
ln -s /usr/lib/jvm/jdk1.7.0_04/jre/lib/i386/libnpjp2.so ~/.mozilla/plugins

If you are using a 64-bit system, remember to change the “i386″ to “amd64“.

Once install, restart your browser. The java applet should run now. To test if Java is working in your browser, go to http://java.com/en/download/testjava.jsp. If installed correctly, you should see a “Your java is working” message.

Do you like what you read here?

Receive the latest update in your inbox.

21 Comments

  • mhenriday

    It’s not always so very easy with Java ! After downloading and extracting the jdk-7u4-linux-x64.tar.gz, moving it from my Downloads folder to /usr/lib/jvm, and installing it according to Damien’s «hard way» instructions, Synaptic shows that the following files are installed :

    openjdk-6-jre
    openjdk-6-jre-headless
    openjdk-6-jre-lib
    openjdk-7-jre
    openjdk-7-jre-headless
    openjdk-7-jre-lib
    oracle-java7-installer

    My question is – which, if any, these files can I uninstall, without negative consequences for my 64-bit Precise setup ? I can mention that upon running «java -version» in a terminal, I get the same result as Damien, above :

    java version “1.7.0_04″
    Java(TM) SE Runtime Environment (build 1.7.0_04-b20)
    Java HotSpot(TM) 64-Bit Server VM (build 23.0-b21, mixed mode)

    Henri

    Reply

  • M Henri Day

    Thanks for your speedy reply, Damien ! What gives me pause is that when I attempt to uninstall the openjdk files, I am informed that even the relevant icedtea files will be removed :

    icedtea-6-plugin
    icedtea-plugin
    icedtea6-plugin

    icedtea-7-jre-cacao
    icedtea-7-jre-jamvm
    icedtea-7-jre-plugin

    These files don’t seem to have anything to do with the oracle-java7-installer file, but still, on the precautionary principle, I’d like to ask if I can expect any problems if I uninstall all the icedtea as well as the openjdk files….

    Thanks again for your kind help !

    Henri

    Reply

    • Damien Oh

       You can remove every instances of the icedtea plugin, since it is only meant for the openjdk packages. I have updated the article with instructions to configure the browser for Oracle Java. Check it out.

      Reply

      • M Henri Day

         Thanks, Damien ! I uninstalled all the openjdk and icedtea packages, which freed something over 180 MB on my harddisk, and so far so good. something to keep in mind in October, when Quantal will be released, as I presume that the openjdk packages will be installed by default when upgrading….

        Henri

        Reply

  • felipito

    I have installed ubuntu 12.04, Im trying to use an app that will not work with openjdk, I installed openjdk, now I can’t get rid of it, it wants to install openjdk 7 as I remove openjdk 6 in synaptic, help!

    Reply

    • Damien Oh

      Install the SunJDK in your system and make it the default. After that, you should be able to uninstall the OpenJDK.

      Reply

      • Anil Duggirala

        ok, what I did is sudo apt-get purge openjdk* following instructions from someone else, then I installed java using the ubuntu ppa. Everything seems to be working now as far as java is concerned.
        thanks

        Reply

  • hwrogers

    hi, tried earlier today to get my java converted to the sunjdk and wound up screwing up horribly, short story i’m on a fresh install of 12.04 (could i have fixed it some other way? don’t care.) followed the “hard way” instructions above, snagged at a typo (mine) but got past and finished install. java -version in the terminal gives the result shown as good above, but the testjava link indicates that java is not working. i find this odd given that the links in the sidebar are all showing fine now when during the process they were “pluggin needed” messages.

    just trying to get runescape to work, have a bet to settle with a friend.

    Reply

    • Damien

      Perhaps you want to try the 32-bit jdk instead of 64-bit. I think that
      Runescape has a compatibility issue with the 64-bit version.

      Reply

  • Danny van der Zande

    You, sir, are a hero!
    I finally have it working in no time thanks to your instructions!
    Thanks a lot!

    Reply

  • Matt

    For some reason it says the file usr/lib/jvm doesn’t exist, do I have to make a new folder called jvm or is it something else? Please help I am a total noob when it comes to using linux.

    Reply

  • danfetter

    One question. If we install java via ‘The hard way’ as described, how do we get notified of updates to the JDK?

    Reply

  • Rilo

    Thanks for this post. It was very helpful to me. Just one remark though. The symbolic link is wrong. Should be (for 64bit):
    ln -s /usr/lib/jvm/java-7-oracle/jre/lib/amd64/libnpjp2.so ~/.mozilla/plugins

    Reply