Install Oracle JDK in Ubuntu

Installing a new JDK with proper update-alternatives setup

This example uses jdk1.6 update 27. Adjust as needed.

Download the self-installing package from Oracle, and go to /usr/lib/jvm and run it. It will unpack the entire jdk in ./jdk1.6.0_27

Rename the directory to "java-6-sun-1.6.0.27", and create a link to it named "java-6-sun":

mv jdk1.6.0_27 java-6-sun-1.6.0.27
ln -s java-6-sun-1.6.0.27 java-6-sun

Create a new text file .java-6-sun-1.6.0.27.jinfo (note the initial period!) with the following contents:

name=java-6-sun-1.6.0.27
alias=java-6-sun
priority=63
section=non-free
 
jre ControlPanel /usr/lib/jvm/java-6-sun/jre/bin/ControlPanel
jre java /usr/lib/jvm/java-6-sun/jre/bin/java
jre java_vm /usr/lib/jvm/java-6-sun/jre/bin/java_vm
jre javaws /usr/lib/jvm/java-6-sun/jre/bin/javaws
jre jcontrol /usr/lib/jvm/java-6-sun/jre/bin/jcontrol
jre keytool /usr/lib/jvm/java-6-sun/jre/bin/keytool
jre pack200 /usr/lib/jvm/java-6-sun/jre/bin/pack200
jre policytool /usr/lib/jvm/java-6-sun/jre/bin/policytool
jre rmid /usr/lib/jvm/java-6-sun/jre/bin/rmid
jre rmiregistry /usr/lib/jvm/java-6-sun/jre/bin/rmiregistry
jre unpack200 /usr/lib/jvm/java-6-sun/jre/bin/unpack200
jre orbd /usr/lib/jvm/java-6-sun/jre/bin/orbd
jre servertool /usr/lib/jvm/java-6-sun/jre/bin/servertool
jre tnameserv /usr/lib/jvm/java-6-sun/jre/bin/tnameserv
jre jexec /usr/lib/jvm/java-6-sun/jre/lib/jexec
jdk HtmlConverter /usr/lib/jvm/java-6-sun/bin/HtmlConverter
jdk appletviewer /usr/lib/jvm/java-6-sun/bin/appletviewer
jdk apt /usr/lib/jvm/java-6-sun/bin/apt
jdk extcheck /usr/lib/jvm/java-6-sun/bin/extcheck
jdk idlj /usr/lib/jvm/java-6-sun/bin/idlj
jdk jar /usr/lib/jvm/java-6-sun/bin/jar
jdk jarsigner /usr/lib/jvm/java-6-sun/bin/jarsigner
jdk javac /usr/lib/jvm/java-6-sun/bin/javac
jdk javadoc /usr/lib/jvm/java-6-sun/bin/javadoc
jdk javah /usr/lib/jvm/java-6-sun/bin/javah
jdk javap /usr/lib/jvm/java-6-sun/bin/javap
jdk jconsole /usr/lib/jvm/java-6-sun/bin/jconsole
jdk jdb /usr/lib/jvm/java-6-sun/bin/jdb
jdk jhat /usr/lib/jvm/java-6-sun/bin/jhat
jdk jinfo /usr/lib/jvm/java-6-sun/bin/jinfo
jdk jmap /usr/lib/jvm/java-6-sun/bin/jmap
jdk jps /usr/lib/jvm/java-6-sun/bin/jps
jdk jrunscript /usr/lib/jvm/java-6-sun/bin/jrunscript
jdk jsadebugd /usr/lib/jvm/java-6-sun/bin/jsadebugd
jdk jstack /usr/lib/jvm/java-6-sun/bin/jstack
jdk jstat /usr/lib/jvm/java-6-sun/bin/jstat
jdk jstatd /usr/lib/jvm/java-6-sun/bin/jstatd
jdk native2ascii /usr/lib/jvm/java-6-sun/bin/native2ascii
jdk rmic /usr/lib/jvm/java-6-sun/bin/rmic
jdk schemagen /usr/lib/jvm/java-6-sun/bin/schemagen
jdk serialver /usr/lib/jvm/java-6-sun/bin/serialver
jdk wsgen /usr/lib/jvm/java-6-sun/bin/wsgen
jdk wsimport /usr/lib/jvm/java-6-sun/bin/wsimport
jdk xjc /usr/lib/jvm/java-6-sun/bin/xjc
plugin xulrunner-1.9-javaplugin.so /usr/lib/jvm/java-6-sun/jre/lib/amd64/libnpjp2.so
plugin mozilla-javaplugin.so /usr/lib/jvm/java-6-sun/jre/lib/amd64/libnpjp2.so

Install the new jdk/jre binaries as proper "alternatives":

egrep "^(jre|jdk)" .java-6-sun-1.6.0.27.jinfo | while read mode name link; do
  update-alternatives --install /usr/bin/$name $name $link 5
done

Use the update-java-alternatives convenience script to switch:

update-java-alternatives -s java-6-sun-1.6.0.27