December 16, 2011

The Fedora "alternatives" scheme

The fedora people had a clever idea, given that there is a plethora of different java brands and versions you might want to run (and even have side by side) on your computer. They provide a command to switch back and forth easily between various java versions you may have installed on your computer.

They do this by manipulating a flock of symbolic links. The critical symbolic links are in the directory /etc/alternatives. As a specific example, the "java" command will (unless you have done something unusual) try to run /usr/bin/java. This is a link to /etc/alternatives/java which in turn is a link to whatever java thing ought to be run.

You could fiddle with all these links by hand, but fedora provides a utility called "alternatives" to take care of this.

If you take the path of least resistance and install OpenJDK from yum or rpm, the links will be made for you. If you install Java from Sun/oracle, their packaging is Fedora agnostic and will not make the links, so you will have to make them yourself.

Setting up the alternative links for Sun Java

Suppose you had just installed Java 7 SDK from Sun, what you would need to do is:

su
## java ##
alternatives --install /usr/bin/java java /usr/java/jdk1.7.0/jre/bin/java 20000
## javaws (32-bit only) ##
alternatives --install /usr/bin/javaws javaws /usr/java/jdk1.7.0/jre/bin/javaws 20000
## Java Browser (Mozilla) Plugin 32-bit ##
alternatives --install /usr/lib/mozilla/plugins/libjavaplugin.so libjavaplugin.so /usr/java/jdk1.7.0/jre/lib/i386/libnpjp2.so 20000
## Java Browser (Mozilla) Plugin 64-bit ##
alternatives --install /usr/lib64/mozilla/plugins/libjavaplugin.so libjavaplugin.so.x86_64 /usr/java/jdk1.7.0/jre/lib/amd64/libnpjp2.so 20000
## Install javac only if you installed JDK (Java Development Kit) package ##
alternatives --install /usr/bin/javac javac /usr/java/jdk1.7.0/bin/javac 20000
alternatives --install /usr/bin/jar jar /usr/java/jdk1.7.0/bin/jar 20000

For the JRE, you would do this:

su
## java ##
alternatives --install /usr/bin/java java /usr/java/jre1.7.0/bin/java 20000
## javaws (32-bit only) ##
alternatives --install /usr/bin/javaws javaws /usr/java/jre1.7.0/bin/javaws 20000
## Java Browser (Mozilla) Plugin 32-bit ##
alternatives --install /usr/lib/mozilla/plugins/libjavaplugin.so libjavaplugin.so /usr/java/jre1.7.0/lib/i386/libnpjp2.so 20000
## Java Browser (Mozilla) Plugin 64-bit ##
alternatives --install /usr/lib64/mozilla/plugins/libjavaplugin.so libjavaplugin.so.x86_64 /usr/java/jre1.7.0/lib/amd64/libnpjp2.so 20000

Verifying what version you have selected, if any

Once this is done, you can verify that the proper things are getting run as follows:
java -version
java version "1.7.0"
Java(TM) SE Runtime Environment (build 1.7.0-b147)
Java HotSpot(TM) Server VM (build 21.0-b17, mixed mode)
  
javaws
Java(TM) Web Start 1.7.0
  [...]
   
javac -version
javac 1.7.0

To check what firefox is doing, restart firefox and then type about:plugins in the URL entry field.

Switching back and forth

Once the links are set up, it is very easy to select what java you want to run (for example you might want for some reason to switch back to using OpenJDK after installing the SDK from Sun). A menu of options to choose from will be given if you type the command:
alternatives --config java

Have any comments? Questions? Drop me a line!

Adventures in Computing / tom@mmto.org