Install Sun Java Alternative in Ubuntu

There are variety of ways you can have Java (JDK) installed on your Ubuntu. You can stick with the default OpenJDK (openjdk-6-jdk package in Ubuntu repository) or install Sun (it’s Oracle now actually) Java (sun-java6-jdk package) from Ubuntu’s multiverse repository using package manager (apt-get). Usually they are couple of revisions behind official Sun JDK releases, but most of the time it is enough.

Sometimes however you want to (or need to) install the latest version of Java. Downloading and unpacking it is quite easy. However making it the default Java installation in your system is harder. You can of course manually change all or part of the symbolic links pointing to java commands, but it just doesn’t feel like the proper way to do it. Why not use the update-java-alternatives command that makes changes system-wide and allows to easily change the default distribution at any time.
Unfortunately update-java-alternatives knows nothing about your manually downloaded JDK, so below are steps that adds new alternative to the registry of JDKs available.

Notice that all the commands presented here should be pasted as one line in your terminal (even if they are split by browser).

Install Java step by step

  1. Download JDK from Sun/Oracle. Choose bin version (not rpm). The file would be in form of jdk-version-platform.bin (in my case it was: jdk-6u25-linux-x64.bin )
  2. Add execution rights to the downloaded file

    $ chmod u+x jdk-6u25-linux-x64.bin

  3. Execute script. It will unpack its content to current directory (press Enter when asked)

    $ ./jdk-6u25-linux-x64.bin

    Now you should have directory i.e. jdk1.6.0_25/ with JDK files in it.
  4. Move unpacked JDK to final destination e.g. /opt/java/

    $ sudo mv jdk1.6.0_25/ /opt/java/

  5. Create symbolic link to that directory that will represent this JDK in general regardless current version. It will ease the future updates of JDK. I can any name. I have chosen sun-java6-manual

    $ cd /opt/java/
    $ sudo ln -s jdk1.6.0_25 sun-java6-manual


  6. Now create another symbolic link in /usr/lib/jvm pointing to previous one.

    $ cd /usr/lib/jvm/
    $ sudo ln -s /opt/java/sun-java6-manual


  7. I assume that you have already installed Sun JDK from Ubuntu repositories before. If this is the case then there should be .java-6-sun.jinfo file in /usr/lib/jvm/ directory. We will copy that file and modify it. If there is no such file you can download it to current directory with this command:

    $ sudo apt-get -d install sun-java6-bin && find /var/cache/apt/archives/ -iname sun-java6-bin* -exec ar -p '{}' data.tar.gz \; | tar -zxO ./usr/lib/jvm/.java-6-sun.jinfo | sudo tee .java-6-sun.jinfo

    You can delete it afterwards. The reason I am not using .java-6-openjdk.jinfo file that probably is present in your Ubuntu installation is that it is slightly different that Sun’s one (some browser plugin entries are missing).

    Copy file and replace path entries that they point to created link in /usr/lib/jvm/

    $ sudo sed s/java-6-sun/sun-java6-manual/ .java-6-sun.jinfo | sudo tee .sun-java6-manual.jinfo

  8. Now edit the beginning of file so it looks like below:

    $ sudo vim .sun-java6-manual.jinfo

    name=sun-java6-manual
    alias=sun-java6-manual
    priority=30


    Choose whatever priority you like.
  9. Now we have to register new alternatives in update-alternatives mechanism.

    $ cat .sun-java6-manual.jinfo | grep -E '^(jre|jdk)' | awk '{print "/usr/bin/" $2 " " $2 " " $3 " 30 \n"}' | xargs -t -n4 sudo update-alternatives --verbose --install

  10. Do the same with plugins

    $ sudo update-alternatives --verbose --install /usr/lib/mozilla/plugins/libjavaplugin.so mozilla-javaplugin.so /usr/lib/jvm/sun-java6-manual/jre/lib/amd64/libnpjp2.so 30

    $ sudo update-alternatives --verbose --install /usr/lib/xulrunner-addons/plugins/libjavaplugin.so xulrunner-1.9-javaplugin.so /usr/lib/jvm/sun-java6-manual/jre/lib/amd64/libnpjp2.so 30

  11. Now we are ready to switch default Java to the new one.

    $ update-java-alternatives -l

    It should list available java alternatives. It may look like:

    java-6-openjdk 1061 /usr/lib/jvm/java-6-openjdk
    java-6-sun 63 /usr/lib/jvm/java-6-sun
    sun-java6-manual 1500 /usr/lib/jvm/sun-java6-manual


  12. Set chosen option with:

    $ sudo update-java-alternatives -s sun-java6-manual

  13. Now confirm that java command is the new installed one

    $ java -version
    java version "1.6.0_25"
    Java(TM) SE Runtime Environment (build 1.6.0_25-b06)
    Java HotSpot(TM) 64-Bit Server VM (build 20.0-b11, mixed mode)


Updating JDK
When you want to update JDK, download and unpack somewhere and change link /opt/java/sun-java6-manual to point to it.

Comments 9 comments

Nick 4 years ago

Thanks, just what I needed :) I think I will repost this at my blog (linking here of course) to have another copy of this tutorial out there for ppl to find.


maciek profile image

maciek 4 years ago from Poland Author

I am glad it was helpful :)


unico 4 years ago

excellent


tim 4 years ago

very confusing


Draken 4 years ago

Impossible with a new installation


very disappointed 4 years ago

Step 7: !? you must be joking


maciek profile image

maciek 4 years ago from Poland Author

Well - command is a bit complex but it does its job


john pitt profile image

john pitt 4 years ago

nice !


Dominic 3 years ago

On latest Ubuntu: step 7 doesn't work (can't get sun-java6-bin even if you force lucid repositories), just copy .java-1.6.0-openjdk-amd64.jinfo and change the last line IcedTeaPlugin.so to libnpjp2.so

Step 9: add hl to the (jre or jdk) command (can't type the or symbol here)

Step 10: no xulrunner, just run the first command (mozilla-javaplugin)

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working