Update: Due to changes by Oracle in licensing for the JDK
all Java packages in the Ubuntu Partner archive to be removed on 2012-02-16. Bummer. It looks like you will have to manually download and install a TAR or RPM version of the JDK from the
Oracle download site.
I've had to do this a lot lately, so it's worth writing a post to remind myself of the steps.
By default Ubuntu packages that depend on Java (JRE or JDK) usually depend on
OpenJDK. OpenJDK seems 98% compatible with the Sun (now Oracle) JDK, but chances are you'll hit an edge case that works only on the Sun JDK.
So let's install the Sun JDK!
The following steps will work with Ubuntu 9.10 (Karmic Koala) or newer.
Install wget
I personally prefer wget over curl for the simple stuff.
sudo apt-get install -y wget
Install python-software-properties
The python-software-properties includes a handy command-line utility called add-apt-repository, which adds packages repositories to you apt sources.list file. Instead of using this utility you can hand edit /etc/apt/sources.list if you prefer.
sudo apt-get install -y python-software-properties
Add the 'Canonical partners' package repository to the package sources list
sudo add-apt-repository "deb http://archive.canonical.com/ubuntu $(lsb_release -s -c) partner"
or manually add the corresponding entry to your /etc/apt/sources.list file. e.g.
deb http://archive.canonical.com/ubuntu natty partner
Rebuild the package index
sudo apt-get update
Avoid the annoying prompt to accept the license
When you install the Sun JDK you will be prompted to read and accept a license. I personally like stuff to install without requiring manual interaction, so do the following to bypass the license prompt (of course we accept the license!):
echo sun-java6-jdk shared/accepted-sun-dlj-v1-1 select true | sudo /usr/bin/debconf-set-selections
echo sun-java6-jre shared/accepted-sun-dlj-v1-1 select true | sudo /usr/bin/debconf-set-selections
Install Sun JDK 6
sudo apt-get install -y sun-java6-jdk sun-java6-plugin
Update the Java system default to the Sun version
Ubuntu allows multiple JVMs to be installed at any one time. To switch the "default" to the Sun JVM do the following:
sudo update-java-alternatives -v -s java-6-sun
And that's it! Phew!
The future is OpenJDK
Oracle are currently saying the official JDK reference implementation from Java 7 onwards will be OpenJDK, so assuming OpenJDK gets fully functional then you won't have to go through all of the steps in this blog post!