Saturday 5 March 2011

Changing JRE's in Ubuntu

If you're working with Java on Ubuntu, you have two main choices as to which runtime to use. The open source OpenJDK and the official Sun runtime. Luckily, due to the alternatives system in Ubuntu (probably inherited from Debian) you can install both of these and switch between them as you see fit.

To see your currently installed JRE's run the command:

$ update-java-alternatives -l

and you should see some output like:

java-6-openjdk 1061 /usr/lib/jvm/java-6-openjdk
java-6-sun 63 /usr/lib/jvm/java-6-sun


This assumes that you've installed both of the runtimes. If you've only got one or the other installed, you will only see one line in the above output.

Now that we know which runtimes are installed, it would also be nice to be able to see which one we are using at the moment. Run the command:

$ java -version

If you see the ouput:

java version "1.6.0_20"
OpenJDK Runtime Environment (IcedTea6 1.9.7) (6b20-1.9.7-0ubuntu1~10.04.1)
OpenJDK 64-Bit Server VM (build 19.0-b09, mixed mode)


Your are running the OpenJDK runtime. If you see:

java version "1.6.0_24"
Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
Java HotSpot(TM) 64-Bit Server VM (build 19.1-b02, mixed mode)


You are running the Sun runtime. As an aside, the OpenJDK version is generally always a few versions behind the official Sun version.

So now that we know which runtimes we have installed and which one we are using at the moment, we can finally switch between them, using the update-java-alternatives command.

To switch to Sun's JRE:

sudo update-java-alternatives --set java-6-sun

To switch to OpenJDK:

sudo update-java-alternatives --set java-6-openjdk

To learn about the OpenJDK project and how it differs from the Sun Java runtime, you can have a look at:

http://openjdk.java.net/
http://en.wikipedia.org/wiki/OpenJDK

The 'update-java-alternatives' command has a lot more options allowing you to choose to switch just the JRE (and keep the JDK the same), or only switch out the browser plugin. To see all of the alternative options, have a look at the man page 'man update-java-alternatives'.

No comments: