Installing Oracle Java JDK 7 and setting JAVA_HOME in Ubuntu (Linux)

This post is not only for people who are new to Linux or Java but also quick notes for me to setup my environment next time I install my system or setup a virtual machine.

Installing OpenJDK can be very simple but not Oracle JDK. You can use one of the two following commands (depending on which version you which to install) to setup OpenJDK in your linux environment.

sudo apt-get install openjdk-6-jdk
or
sudo apt-get install openjdk-7-jdk

But I have always used Oracle Sun JDK for my development. While Oracle JDK (earlier Sun JDK) is under the Binary Code License (earlier Sun License), OpenJDK is under GPL with a linking exception. From JDK version 7, Oracle has planned to support OpenJDK and withdraw the Operating System Distributor License for Java. This has resulted in a withdrawal of Oracle JDK from the repositories of Linux distributions. Therefore you can’t just use apt-get to install Oracle JDK.

Here is the stackoverflow link to help you choose between the Oracle JDK or OpenJDK.

But users who prefer to use the Oracle JDK binaries over OpenJDK builds packaged in their Linux distributions of choice can of course download the package from http://oracle.com/java under the same terms as users on other platforms. Here is the direct link to JavaSE downloads.

Coming the point I have downloaded JDK 7 (update 4) for Linux x86 (32-bit) – file: jdk-7u21-linux-i586.tar.gz

Step 1: After downloading the file open the terminal and navigate to the file

tar -xf jdk-7u21-linux-i586.tar.gz

This will extract and create a jdk folder at your current path.

Step 2: Create a location to keep your new JDK . I prefer and usually use /usr/lib/jvm/

You may need root permission to create the /usr/lib/jvm (hence use sudo).

sudo mkdir /usr/lib/jvm 

Step 3: Move the extracted jdk folder to /usr/lib/jvm/

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

Step 4: Now we have to setup our system to use refer to our new jdk

sudo update-alternatives --install "/usr/bin/java" "java" \
"/usr/lib/jvm/jdk1.7.0_21/bin/java" 1

sudo update-alternatives --config java

And also register Firefox Java Plugin

sudo update-alternatives --install "/usr/lib/mozilla/plugins/libjavaplugin.so" \
"mozilla-javaplugin.so" "/usr/lib/jvm/jdk1.7.0_21/jre/lib/i386/libnpjp2.so" 1

sudo update-alternatives --config mozilla-javaplugin.so

ALL DONE. You can test your java install by

java -version

Here is a small screen capture of what happened on my screen when I went through above steps

Oracle JDK Install

Finally if you need to add JAVA_HOME variable, you can do so by adding it to the .bashrc file in your home directory

Open .bashrc file using an editor. If you use VI then
vi ~/.bashrc

and add the following 2 lines in your .bashrc file.

JAVA_HOME=/usr/lib/jvm/jdk1.7.0_21/
export JAVA_HOME

There may be other ways to install, but this is what I have followed always.

Cheers

Tagged , , ,
  • Sathishkumar

    Thanks for the guide..

    • Manish

      Happy to know it helped you.

  • Torsten

    Thanks m8!

    • Manish

      You are welcome!

  • Cristiano

    I’m a new user of Ubuntu. i have Ubuntu 10.04.4
    i follow your steps but i stopped at step 4 it gives me an error.

    update-alternatives: erro: ligação da alternativa não é absoluta como deveria ser: “/usr/bin/java”
    that means :
    “Alternatively the connection is not absolute as it should be”

    sorry for my bad English, it’s not my first language.

    could you help me?
    thanks!
    ;)

    • Manish

      Just as per our email conversation, I will post out the problem that you faced here, so that it can help others who face similar problem.

      The problem was with using option “-install” where we should have used double hash “--install”

  • Cristiano

    Thanks Manish, you helped me a lot!

    wish you luck!

    • Manish

      I am glad I could help.

  • Simon

    I’m not an Ubuntu user, but in Debian we have “java-package”, this is an easy to use tool with which you can just create a deb package from a downloaded version of the JRE/JDK. It involves less work, it’s easier to update/remove and it’s integrated with the rest of the system. I’m quite sure this application is available in Ubuntu distributions too. It’s usage is explained here: http://wiki.debian.org/JavaPackage

    • Manish

      Hi there, Thanks for the tip. If this is the case, I will try it out today and if that is an easier process I will update the post accordingly.

  • Bruno

    There is an “installer” for Oracle JDK7 : https://launchpad.net/~webupd8team/+archive/java

    It works the same way as the flash plugin installer.

  • Geroen

    2 additional tips:

    (1) It’s better to create a symlink to the /usr/lib/jvm/jdk1.7.0_04/ folder, for instance: /usr/lib/jvm/latest/ . That way, you don’t have to update your alternatives each time you upgrade your JDK, considering you can add the symlink path to your alternatives. Same goes for your JAVA_HOME variable and firefox plugin, you don’t have to alter those later on either.

    You could create that via this command: sudo ln -s /usr/lib/jvm/jdk1.7.0_04 /usr/lib/jvm/latest

    (2) This guide is all well if you just want to use the java runtime binary. However, there are other binaries too that could be or even need to be set up with the alternatives. Think about javac, for instance. Or javaws.
    Even worse: if you have 1 dependency for java in something you install via the repositories, it’ll include the openjdk as dependency, and install it. So, if you don’t configure javac to use your Oracle JDK’s one, you’ll be using different JDK’s for running java programs (using the java binary) and compiling them command line (using the javac binary – for which you will be using the OpenJDK). For most users, this won’t matter, but I’ve been doing this setup for some time now in Linux, and I’ve encountered times where this could bork your setup. PS: I’m a Java developer, maybe that makes me biased.

  • Pingback: Setting up Nutch 2.0 with MySQL to handle UTF-8 « Search and NLP for CJK

  • Roberto

    Thanks you! Worked great for me.

    Can’t be explain better ;)

    • Manish

      Happy to know that it helped you. Cheers, Manish

  • Kartik A

    sudo update-alternatives –install “/usr/lib/mozilla/plugins/libjavaplugin.so” \
    “mozilla-javaplugin.so” “/usr/lib/jvm/jdk1.7.0_09/jre/lib/i386/libnpjp2.so” 1

    On typing this command in the terminal I get a error saying that
    update-alternatives: error: alternative path /usr/lib/jvm/jdk1.7.0_09/jre/lib/i386/libnpjp2.so doesn’t exist.
    Mine is 09 instead of 04

    • Manish

      Hi Karthik,
      I will have to test with the latest version of JDK. that is 1.7.0_09. In case you don’t plan to use java in your browser (mainly for applets), then you can safely ignore this for now.
      Cheers

  • Tomasz

    Thanks it’s help me :)

  • mich

    sudo update-alternatives –install “/usr/bin/java” “java” “/usr/lib/jvm/jdk1.7.0_21/bin/java” 1

    gives me:

    update-alternatives: error: alternative link is not absolute as it is required: “/usr/bin/java”

    could you help?

    • Manish

      Hi Mich

      I just checked, the command contains invalid double quote characters “ ”. I think this is an encoding issue with my blog post which I will fix now. The command is absolutely correct, you just need to use the other double quotes like " ". That should work!

      Cheers

    • Manish

      I have fixed the encoding issue.

      So instead of right double quote and left double quote. I will just be normal double quotes.

      It should work fine now. Thanks for raising the issue.

      • mich

        Perfect! It’s working now.
        (I didn’t notice this small encoding issue durning my copy-paste)

        Thanks for your help and fast response, take care!

  • Alex

    Hi,

    Your command even with correct quotes as suggested by you is not working.
    Is giving the same error as already mentioned here.

    • Manish

      Hi Alex,

      As Mich mentioned, it worked for him after fixing up the quotes. Your error might be different. Can you paste the command here that you are trying to execute and let’s discuss about it then.

      I just tried the commands again on a fresh install of ubuntu in a VM and it worked fine.

  • Alex

    Hi,

    This is the command I am using:
    sudo update-alternatives –install “/usr/bin/java” “java” \ “/usr/lib/jvm/jdk1.7.0_21/bin/java” 1

    • Manish

      Hi Alex,

      I should point out that the command is absolutely fine but the \ after “java” is just to break the command in 2 lines. If you are copy paste the whole command in one line, then remove that slash (\) and just use the command as

      sudo update-alternatives –install “/usr/bin/java” “java” “/usr/lib/jvm/jdk1.7.0_21/bin/java” 1

      Cheers
      Manish

  • Alex

    Hi Manish,

    Thank you, it worked now.

    • Manish

      Thanks Alex. I am glad it helped you. Cheers

  • rao

    Hi Manish
    thanks for the comments in the blog
    i am new to ubuntu & have ubuntu 12.04, needs to install rubymine, but rubymine 5.4.1 require sun jdk
    could you please inform step by step installation of sun jdk and rubymine, for which of your suggestions i will be greatful to you
    plz.,send mail,if possible
    thanks
    rao

  • http://gravatar.com/bionik6 bionik6

    Thank you, that do the trick, I install IntelliJIdea12 successfully :D

  • Rahul

    really nice tutorial dude. had issues while doing it. but all of them I could overcome by reading the comments and your clean replies :) cheers man.!!

    • Manish

      Thanks Rahul. I appreciate your comment.

  • Saurabh jain

    Thanks Manish…

  • Mahesh

    Tried searching many websites but could not get proper instructions. Very useful guide, Thank you.

    • Manish

      Thanks Mahesh. Appreciate your comment.

  • rahul

    I am not able to understand the 4th step could you please guide me the installation

  • Kunaal

    Thanks for this clear and precise explanation. My appreciated.

    • rahul

      get me solution

  • http://gravatar.com/shelleyfrank shelleyfrankMichael

    Your guide worked for me thanks.

  • http://aakritty.wordpress.com aakritty

    Thanks!!

  • sumit

    This blog is very helpful.

    Thanks

  • http://twitter.com/iganapolsky Igor Ganapolsky (@iganapolsky)

    Excellent guide. Very straightforward. Helped me update my java in less than 5 minutes!

  • Omar Ebrahim

    Not sure why but I’m getting:

    omar@omar-Aspire-5742:~/Downloads$ sudo update-alternatives –install “/usr/bin/java” “java” “/usr/lib/jvm/jdk1.7.0_25/java” 1
    update-alternatives: error: alternative path /usr/lib/jvm/jdk1.7.0_25/java doesn’t exist.

    • K

      Your path is wrong. It should be /usr/lib/jvm/jdk1.7.0_25/bin/java

      • Manish

        Thanks K.

  • Prabhu

    Hai manish can help me in this area.

    VPCEH25EN:~$ sudo update-alternatives –install “/usr/bin/java” “java” “/usr/lib/jvm/jdk1.7.0_21/bin/java” 1
    update-alternatives: error: alternative path /usr/lib/jvm/jdk1.7.0_21/bin/java doesn’t exist
    VPCEH25EN:~$ sudo update-alternatives –install “/usr/bin/java” “java” “/usr/lib/jvm/jdk1.7.0_21/bin/java” 1
    update-alternatives: error: alternative link is not absolute as it should be: “/usr/bin/java”

    Thanks in advance.