Installing and switching between versions of Java on Ubuntu is a relatively simple process (with a couple of caveats), you just need to know the right commands. Considering that there was a time when I didn’t know this either I thought I’d share what I know (shouldn’t take long :)).
Anyways, first thing first. When we talk about installing Java on Ubuntu we are of course talking about Sun Java. You can also install Open JDK if you want, but most people will use the Sun version for their Java development. Of course these days there are several versions of Sun Java that are in mainstream use; well basically 2, Java1.5 and Java6 (I am sure things will be even more interesting when Java 7 comes out). Regardless of which version of the JDK you want to install the process to do this on Ubuntu is the same. You need to know what package you want to install and then you need to use our old friend apt-get.
In the case of the Sun JDK you will want to install either (or both) of:
The commands for this will be:
Once you put in your password apt-get will go ahead and do it’s thing. The only thing to watch out for is that with Java you might get a license screen popping up during the installation (i.e. it won’t just finish by itself, so don’t go away), you will need to accept. That’s it, you now have one or several version of Java installed.
Switching Between Versions
Of course, if you have more than one version installed it is probably for a reason and you will need to switch between them at some point. Ubuntu has a very handy command just for this, update-java-alternatives. Using this command is simple:
Which will produce output similar to the following:
You can now pick one of the alternatives that were printed out and use the same command, with a different switch, to make Ubuntu use it as it’s default Java installation:
This will output a bunch of stuff:
There are two things to note about the command.
- It needs root privileges, so you will need to execute it with sudo
- The name of the java version you pass to the command is one of the things that was printed out when you ran update-java-alternatives –l (i.e. java-6-sun or java-1.5.0-sun)
This is all you need to do to have your whole Ubuntu installation use a different version of Java. That’s all great, but there is one big problem.
The Built-In FAIL And How To Fix It
Many Java applications written these days rely on the existence of the JAVA_HOME environment variable (they use this variable to know which Java they should execute with). Unfortunately none of the commands we ran above set the JAVA_HOME environment variable. Instead you will normally have to wait until the first time you execute a Java application which fails when it tries to find JAVA_HOME before you realize that you will need to set this yourself.
Even then things are not quite that simple. The update-java-alternatives command relies quite heavily on symlinks so it may take a while to even figure out where your current Java is installed. Once you do figure it out and set your JAVA_HOME environment variable, you have to remember to update it if you decide to switch java versions later (by using the update-java-alternatives command).
I will try to save you some pain and tell you where things live and then show you how I set my JAVA_HOME so that it is automatically (almost) updated when you switch Java versions. So, the first thing you would need to do to figure out where things live is to run the following command:
Which tells you that your java executable lives in:
Of course if you go and have a look you will see that it is actually a symlink to:
That in turn is also a symlink to something like (depending on your java version):
We’re almost there but not quite. You need to go back a few directories from the path above, more specifically you need to go to:
This is where all the JVMs that are on your system will be found, you will also notice that java-6-sun in this directory is actually a symlink to the java-6-sun-18.104.22.168 (your actual version may differ) directory. The story for java-1.5.0-sun is similar. So, now you know that if you’re currently using Java 6 then your JAVA_HOME path should be:
To make sure this path persists between shutdowns, you will need to put it in your .bashrc file. But, as I mentioned you don’t want to just hard-code it as you would need to come and change it when you switch versions. So instead you can add the following line to the end of your .bashrc file:
What this does is automatically work out what your JAVA_HOME path should be (and sets it) depending on what version of Java you’re currently using. It finds where your java interpreter lives and follows all the symlinks if there are any (and we know there are several). It then uses awk to extract the path before any bin folders which will be the path to your JAVA_HOME. The reason all of this works is because:
- the java compiler javac will always be in the bin folder of your JDK as opposed to the bin folder of your JRE (like the java executable itself could be), so extracting the path before the bin folder using awk, gives is the path to what JAVA_HOME should be
- using readlink –f, allows us to follow multiple levels of symlinks, which is handy in this case since that’s what we need to do to actually get the real path of where the javac compiler lives
The only thing to remember with this is that once it is in your .bashrc, every time you switch Java versions you will need to close the terminal and open a new one, or source your .bashrc again to make sure the JAVA_HOME path is updated. It is not a perfect solution but it is a decent one and it does the job. However if someone knows of a way to do this without having to start a new terminal or source your profile, do leave a comment and share your knowledge with everyone.
That’s all you need to know to install and work with Java on Ubuntu like a pro :).
Image by Refracted Moments