Buy me a BeerBuy me a Beer
Found something on this little site you've found useful? Buy me a beer!
Alternatively use one of my referral links on my thanks page.
Alternatives is a program that is there to help maintain links on the system. I used to use symbolic links and change those to the new version of software as and when required. However it seems the 'proper' way to do this is with the alternatives system.

I'm still not sure I fully understand the advantages of this system over using regular symlinks, but I'm trying to get in the habit of using alternatives in the hope I will get a better understanding of why it is preferable to symlinks maintained by me in /usr/bin. Alternatives does not attempt to change that but adds a series of links to /etc.

It is then possible at a later date to use alternatives to switch between programs for a given link in /usr/bin.

So, for using different Java SDKs and switching easily/quickly....

Starting Java config in alternatives:
[root@fatbeast java]# alternatives --config java

There is 3 program that provides 'java'.

  Selection    Command
*  1           /usr/lib/jvm/jre-1.6.0-openjdk.x86_64/bin/java
   2           /usr/lib/jvm/jre-1.5.0-gcj/bin/java
 + 3           /usr/lib/jvm/jdk1.6.0_14/jre/bin/java

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

So we see the alternatives system is currently aware of 3 possible 'java' commands, each from a different SDK. I want to use the Sun official SDK, so download and unpack to /usr/loca/java/<jdk_x_x_>/

Having unpacked the binary download I need to setup alternatives so I can switch the java command to refer to the Sun JDK instead of one provided from the Fedora repositories via yum.

[root@fatbeast java]# alternatives --install /usr/bin/java java /usr/local/java/jdk1.5.0_22/bin/java 2000

Have a look at the man page of alternatives to understand what the command does above in full, but in short I provide the name of the symlink, the name of this setup and where to link to in that order.

Of course there are some other handy java related tools provided in the SDK, so let's set those up too while we're at it.

[root@fatbeast java]# alternatives --install /usr/bin/javac javac /usr/local/java/jdk1.5.0_22/bin/javac 2000
[root@fatbeast java]# alternatives --install /usr/bin/javaw javaw /usr/local/java/jdk1.5.0_22/bin/javaw 2000

Now I should be able to use alternatives to change the java command to reference the Sun SDK:

[root@fatbeast java]# alternatives --config java

There is 4 program that provides 'java'.

Selection Command
* 1 /usr/lib/jvm/jre-1.6.0-openjdk.x86_64/bin/java
2 /usr/lib/jvm/jre-1.5.0-gcj/bin/java
+ 3 /usr/lib/jvm/jdk1.6.0_14/jre/bin/java
4 /usr/local/java/jdk1.5.0_22/bin/java

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

Selecting 4 means any subsequent CLI calls to java call the command in the JDK I installed:

[root@fatbeast java]# java -version
java version "1.5.0_22"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_22-b03)
Java HotSpot(TM) 64-Bit Server VM (build 1.5.0_22-b03, mixed mode)