Fedora Core 8 using ‘alternatives’ to install Sun’s jdk

I have some old-ish java software that I needed to get going on Fedora Core 8. The icedtea java stuff never seems to work for me. I usually pull down the java sdk from Sun and then manually change all the things in the path, mozilla directories and my environment variables until I can get java to work. I decide this time to invest some time trying to figure out the right way to do it.
First I installed java (1.6 sdk), Sun now puts their stuff in /usr/java. There is a /usr/java/default link to a latest directory which links to the version I installed.


[root@h ~]# ls -l /usr/java
total 16
lrwxrwxrwx 1 root root   16 2008-08-07 13:18 default -> /usr/java/latest
drwxr-xr-x 9 root root 4096 2008-08-07 13:18 jdk1.6.0_07
lrwxrwxrwx 1 root root   21 2008-08-07 13:18 latest -> /usr/java/jdk1.6.0_07

FC8 has a utility called alternatives. This utility controls the links to standard utilities so that your machine can have different packages of these utilities and you can select which package you use. For example, java (the run time VM):

lrwxrwxrwx 1 root root 22 2008-08-01 09:44 /usr/bin/java -> /etc/alternatives/java
lrwxrwxrwx 1 root root 26 2008-08-18 16:30 /etc/alternatives/java -> /usr/java/default/bin/java

The alternatives utility set this link up. Alternatives has a concept of a master links and slave links. Master links have a name (e.g. java). The master’s slave links follow the master link. When you select a different link with alternatives, it changes the master link and the slave links.

To see the different options for a master link, you can run ‘alternatives –display <name>’. For example:


]# alternatives --display java
java - status is manual.
 link currently points to /usr/java/default/bin/java
/usr/lib/jvm/jre-1.5.0-gcj/bin/java - priority 1500
 slave keytool: /usr/lib/jvm/jre-1.5.0-gcj/bin/keytool
 slave rmiregistry: /usr/lib/jvm/jre-1.5.0-gcj/bin/rmiregistry
 slave jre_exports: /usr/lib/jvm-exports/jre-1.5.0-gcj
 slave jre: /usr/lib/jvm/jre-1.5.0-gcj
/usr/java/default/bin/java - priority 2000
 slave keytool: /usr/java/jdk1.6.0_07/bin/keytool
 slave rmiregistry: /usr/java/jdk1.6.0_07/bin/rmiregistry
 slave jre_exports: /usr/lib/jvm-exports/jre-1.5.0-gcj
 slave jre: /usr/java/default/jre
Current `best' version is /usr/java/default/bin/java.

The output here says that the java link is set manually. (Automatic would set it best on the priority). The link currently points to /usr/java/default/bin/java. You can also see the slave links that go along with it.

To change the link, you can run ‘alternatives –config java’.


]# alternatives --display java
java - status is manual.
 link currently points to /usr/java/default/bin/java
/usr/lib/jvm/jre-1.5.0-gcj/bin/java - priority 1500
 slave keytool: /usr/lib/jvm/jre-1.5.0-gcj/bin/keytool
 slave rmiregistry: /usr/lib/jvm/jre-1.5.0-gcj/bin/rmiregistry
 slave jre_exports: /usr/lib/jvm-exports/jre-1.5.0-gcj
 slave jre: /usr/lib/jvm/jre-1.5.0-gcj
/usr/java/default/bin/java - priority 2000
 slave keytool: /usr/java/jdk1.6.0_07/bin/keytool
 slave rmiregistry: /usr/java/jdk1.6.0_07/bin/rmiregistry
 slave jre_exports: /usr/lib/jvm-exports/jre-1.5.0-gcj
 slave jre: /usr/java/default/jre
Current `best' version is /usr/java/default/bin/java.
[root@fstats ~]# alternatives --config java

There are 2 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
   1           /usr/lib/jvm/jre-1.5.0-gcj/bin/java
*+ 2           /usr/java/default/bin/java

Enter to keep the current selection[+], or type selection number:

Then you just pick the number you want. The problem now is how do you have Sun’s java as an alternative? After playing around for a bit, here’s the script that worked for me:


#!/bin/bash

alternatives --verbose --install /usr/bin/java java /usr/java/default/bin/java 2000 \
  --slave /usr/lib/jvm/jre jre /usr/java/default/jre \
  --slave /usr/lib/jvm-exports/jre jre_exports /usr/lib/jvm-exports/jre-1.5.0-gcj \
  --slave /usr/bin/keytool keytool /usr/java/jdk1.6.0_07/bin/keytool \
  --slave /usr/bin/rmiregistry rmiregistry /usr/java/jdk1.6.0_07/bin/rmiregistry

alternatives --verbose --install /usr/bin/javac javac /usr/java/default/bin/javac \
2000  --slave /usr/lib/jvm/java java_sdk /usr/java/default \
  --slave /usr/lib/jvm-exports/java java_sdk_exports /usr/lib/jvm-exports/java-1.5.0-gcj \
  --slave /usr/bin/javadoc javadoc /usr/java/default/bin/javadoc \
  --slave /usr/bin/javah javah /usr/java/default/bin/javah \
  --slave /usr/bin/jar jar /usr/java/default/bin/jar \
  --slave /usr/bin/jarsigner jarsigner /usr/java/default/bin/jarsigner \
  --slave /usr/bin/appletviewer appletviewer /usr/java/default/bin/appletviewer \
  --slave /usr/bin/rmic rmic /usr/java/default/bin/rmic

There is stuff out on the web that kind of explains this but I had trouble with a few things. Here is the man entry for the config command:


alternatives [options] --install link name path priority [--slave link name path]... [--initscript service]

The link is the link on your path (e.g. /usr/bin/java) not the link in the /etc/alternatives directory. The name is the master link name (e.g. java). The path is where you new stuff is (e.g. /usr/java/default/bin/java). The slave links have names too.

Terrific, how do you find out what all nasty slave links and names should be in your ‘alternatives –config’ command line? You can look at ‘alternatives –display’ output but it doesn’t tell you install link (e.g. /usr/bin/java). I don’t know why this is — you should be able to use the tool to display everything you need to install an alternative.

Anyway, here’s the punch line. You can goto the /var/lib/alternatives directory and there is a file in there for every master link. For example, here is my /var/lib/alternatives/java:


auto
/usr/bin/java
keytool
/usr/bin/keytool
orbd
/usr/bin/orbd
pack200
/usr/bin/pack200
policytool
/usr/bin/policytool
rmid
/usr/bin/rmid
rmiregistry
/usr/bin/rmiregistry
servertool
/usr/bin/servertool
tnameserv
/usr/bin/tnameserv
unpack200
/usr/bin/unpack200
jre_exports
/usr/lib/jvm-exports/jre
jre
/usr/lib/jvm/jre
java.1.gz
/usr/share/man/man1/java.1.gz
keytool.1.gz
/usr/share/man/man1/keytool.1.gz
...

Note that there are a bunch of entries that aren’t listed by ‘alternatives –config’ or ‘alternatives –display’.  I ignored those when I wrote my ‘alternatives –install’ script.

I am sure that there are things that I did wrong here but it seems to work and allows me to switch back and forth between Icedtea and Sun’s java.

Leave a Reply

Your email address will not be published. Required fields are marked *