How to Install Java 7 on Ubuntu

I just went through the fairly painful process of installing Java 7 on Ubuntu. I say “painful” because there’s no dpkg yet available that I could find and most of the tutorials I found only update the java(1) binary and not all of the executables for the JDK. So, I’m going to post proper instructions on how to do this for the children (they are the future, after all).

First, download the .bin installer file from here. Then unpack it via:

sh jdk-7-ea-bin-b98-linux-x64-17_jun_2010.bin

It will ask you to accept the license by typing yes at the end. Do this. It will then produce a directory called jdk1.7.0. Now you’ll want to put that directory in its proper location in /usr/lib/jvm:

sudo mv jdk1.7.0 /usr/lib/jvm/
sudo chown -R root:root /usr/lib/jvm/jdk1.7.0

Now you will want to download this script and run it.

Script to install all alternatives for JDK 7

curl http://gist.github.com/raw/445930/3349713e5eae62b14428894c4971331526129df5/in... > install_java7_alternatives
sudo ./install_java7_alternatives

Next, you will want to download this file and install it into /usr/lib/jvm/.jdk1.7.0.jinfo:

Info file for update-java-alternatives

curl http://gist.github.com/raw/445929/794c6775d16a9b7ed03b05fd318cf62362bcaa1a/.j... > .jdk1.7.0.jinfo
sudo mv .jdk1.7.0.jinfo /usr/lib/jvm/
sudo chown root:root /usr/lib/jvm/.jdk1.7.0.jinfo

Finally, run the following:

sudo update-java-alternatives -s jdk1.7.0

Now you have JDK 7 installed and ready for use. You can switch back and forth between JVMs with the aforementioned update-java-alternatives command.