Alternatives

by jenny on 04 September 2007 - 03:47pm in

Choosing an alternative

% sudo update-alternatives --config <name>

...where <name> is the name of a symlink in /etc/alternatives. Some of the ones I've used:

editor
x-www-browser
java

You are then prompted to select among the various alternatives available. If the desired alternative is not listed, it will have to be installed.

Installing a new editor

wtf is vim.tiny??

sudo update-alternatives --install /usr/bin/editor editor /usr/bin/vi 1

Note: I was surprised to find that the /usr/bin/editor symlink already existed and pointed into the /etc/alternatives directory before I ran the above command.
jenny> ll /usr/bin/editor
lrwxrwxrwx 1 root root 24 Oct 16 16:57 /usr/bin/editor -> /etc/alternatives/editor*

A little further investigation with the following command revealed that /usr/bin seems to be the preferred place for these and all the /etc/alternatives are represented there.

find /usr/bin -type l -exec ls -l {} \;|grep alternatives|less

Setting up Sun Java

Install your new jdk in /usr/local. Set a temporary JAVA_HOME variable on the command line, like so:

JAVA_HOME=/usr/local/jdk1.5.0_06

Install a new alternative for java, with the other various java programs as slaves:

sudo update-alternatives \
--install /usr/bin/java java $JAVA_HOME/bin/java 1 \
--slave /usr/bin/javac javac $JAVA_HOME/bin/javac \
--slave /usr/bin/javadoc javadoc $JAVA_HOME/bin/javadoc \
--slave /usr/bin/jar jar $JAVA_HOME/bin/jar \
--slave /usr/bin/keytool keytool $JAVA_HOME/bin/keytool \
--slave /usr/local/java java_home $JAVA_HOME

If you screwed this up, it can be undone (this will remove the slaves as well):

sudo update-alternatives --remove java $JAVA_HOME/bin/java

You can now happily choose the proper java alternative and all the slaves will follow suit:

update-alternatives --config java

There are 3 alternatives which provide `java'.

  Selection    Alternative
-----------------------------------------------
+        1    /usr/bin/gij-4.2
          2    /usr/local/jdk1.6.0_01/bin/java
*         3    /usr/local/jdk1.5.0_06/bin/java

Press enter to keep the default[*], or type selection number:

Here's the part I don't get. It seems update-alternatives does not automatically create the generic name symlinks for you. It should. This only has to be done the first time you install a jdk:

sudo ln -s /etc/alternatives/java /usr/bin/java
sudo ln -s /etc/alternatives/javac /usr/bin/javac
sudo ln -s /etc/alternatives/javadoc /usr/bin/javadoc
sudo ln -s /etc/alternatives/jar /usr/bin/jar
sudo ln -s /etc/alternatives/keytool /usr/bin/keytool
sudo ln -s /etc/alternatives/java_home /usr/local/java

Now set JAVA_HOME for real in your .bashrc:

JAVA_HOME=/usr/local/java

This will be updated along with the rest of the slaves when you choose a new alternative.

it's work!

it's work!