Which Java on Ubuntu

Ubuntu supplies the "OpenJDK" flavour of Java by default. This may be fine for most purposes but you will find several development tools demand that you use Sun/Oracle's own Java. Now finding it and installing it is not a great problem. There are several FAQs for that. But every so often the system gets confused about which Java it is supposed to be using.

You can list the available installed Javas on recent versions of Ubuntu with this command

$ sudo update-java-alternatives -l 
java-6-openjdk 1061 /usr/lib/jvm/java-6-openjdk
java-6-sun 63 /usr/lib/jvm/java-6-sun

And set the preferred one with this:

$ sudo update-java-alternatives -s  java-6-sun

You might also check that it got it right with

$ ls -l  /etc/alternatives/java*
lrwxrwxrwx 1 root root 36 2010-10-27 10:34 /etc/alternatives/java -> /usr/lib/jvm/java-6-sun/jre/bin/java
lrwxrwxrwx 1 root root 46 2010-10-27 10:34 /etc/alternatives/java.1.gz -> /usr/lib/jvm/java-6-sun/jre/man/man1/java.1.gz
lrwxrwxrwx 1 root root 33 2010-10-27 10:34 /etc/alternatives/javac -> /usr/lib/jvm/java-6-sun/bin/javac
lrwxrwxrwx 1 root root 43 2010-10-27 10:34 /etc/alternatives/javac.1.gz -> /usr/lib/jvm/java-6-sun/man/man1/javac.1.gz
lrwxrwxrwx 1 root root 35 2010-10-27 10:34 /etc/alternatives/javadoc -> /usr/lib/jvm/java-6-sun/bin/javadoc
lrwxrwxrwx 1 root root 45 2010-10-27 10:34 /etc/alternatives/javadoc.1.gz -> /usr/lib/jvm/java-6-sun/man/man1/javadoc.1.gz
lrwxrwxrwx 1 root root 33 2010-10-27 10:34 /etc/alternatives/javah -> /usr/lib/jvm/java-6-sun/bin/javah
lrwxrwxrwx 1 root root 43 2010-10-27 10:34 /etc/alternatives/javah.1.gz -> /usr/lib/jvm/java-6-sun/man/man1/javah.1.gz
lrwxrwxrwx 1 root root 33 2010-10-27 10:34 /etc/alternatives/javap -> /usr/lib/jvm/java-6-sun/bin/javap
lrwxrwxrwx 1 root root 43 2010-10-27 10:34 /etc/alternatives/javap.1.gz -> /usr/lib/jvm/java-6-sun/man/man1/javap.1.gz
lrwxrwxrwx 1 root root 40 2010-06-09 11:30 /etc/alternatives/java-rmi.cgi -> /usr/lib/jvm/java-6-sun/bin/java-rmi.cgi
lrwxrwxrwx 1 root root 39 2010-02-10 17:45 /etc/alternatives/java_vm -> /usr/lib/jvm/java-6-sun/jre/bin/java_vm
lrwxrwxrwx 1 root root 38 2010-10-27 10:34 /etc/alternatives/javaws -> /usr/lib/jvm/java-6-sun/jre/bin/javaws
lrwxrwxrwx 1 root root 48 2010-10-27 10:34 /etc/alternatives/javaws.1.gz -> /usr/lib/jvm/java-6-sun/jre/man/man1/javaws.1.gz

Of course none of this helps you set JAVA_HOME which is a shame. There seems to be no standard way of doing this on Ubuntu so it is usually left up to the developer to set it themselves.

As an aside - has anyone come up with a good name for Sun/Oracle? I know that the business itself says "Oracle" - but puts "Sun" and "Oracle" on the side of their big machines. We shall see how long that lasts.