working with multiple JVM versions

Redhat Linux has a small footprint jre installed by default called gij.
If you install jdk 1.4.x or 5 and want or need to have a nice way to handle switching among them, 'alternatives' does this pretty well. I am no expert on using this command which is primarily used by rpm packaging processes, but....:

As root, run

 alternatives --display java 

... and (on Redhat) you should see something like

java - status is auto.
 link currently points to /usr/lib/jvm/jre-1.4.2-gcj/bin/java
/usr/lib/jvm/jre-1.4.2-gcj/bin/java - priority 1420
 slave rmiregistry: /usr/lib/jvm/jre-1.4.2-gcj/bin/rmiregistry
 slave jre_exports: /usr/lib/jvm-exports/jre-1.4.2-gcj
 slave jre: /usr/lib/jvm/jre-1.4.2-gcj

.. which means that alternatives will automatically ('status is auto') use the 'best' version based on priority settings (1420) ... higher number is higher priority

Indeed, look at the version:

> java -version

java version "1.4.2"
gcj (GCC) 3.4.3 20050227 (Red Hat 3.4.3-22.1)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

... this is the gij version to which I was referring

So, I add the 1.4.2 jvm and want alternatives to recognize this new version as an alternative (get it?):

alternatives --install /usr/bin/java java /usr/java/j2sdk1.4.2_08/bin/java 2000 \
     --slave /usr/bin/rmiregistry rmiregistry /usr/java/j2sdk1.4.2_08/bin/rmiregistry

I also added jdk 1.5... and so

alternatives --install /usr/bin/java java /usr/java/j2sdk1.5.0_04/bin/java 1800 \
     --slave /usr/bin/rmiregistry rmiregistry /usr/java/j2sdk1.5.0_04/bin/rmiregistry

Now if I check the version:

> java -version

java version "1.4.2_08"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_08-b03)
Java HotSpot(TM) Client VM (build 1.4.2_08-b03, mixed mode)

The Cool Part

You can switch to another alternative with the config switch:


> alternatives --config java


There are 3 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
   1           /usr/lib/jvm/jre-1.4.2-gcj/bin/java
*+ 2           /usr/java/j2sdk1.4.2_08//bin/java
   3           /usr/java/jdk1.5.0_04/bin/java


(yeah, I know I have double slashes in my 1.4.2 path, thanks)
... now you can choose a number to switch to that alternative. The * refers to the current 'best' alternative based on priority numbers and the + refers to the current configured 'working' alternative: the one that will run now.

So, if I choose alternative #3:

> java -version

java version "1.5.0_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
Java HotSpot(TM) Client VM (build 1.5.0_04-b05, mixed mode, sharing)

> alternatives --config java


There are 3 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
   1           /usr/lib/jvm/jre-1.4.2-gcj/bin/java
*  2           /usr/java/j2sdk1.4.2_08//bin/java
 + 3           /usr/java/jdk1.5.0_04/bin/java


Note that choosing to config an alternative 'turns off' auto mode. And auto mode can be reset with the auto switch:

> alternatives --auto java
> alternatives --config java


There are 3 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
   1           /usr/lib/jvm/jre-1.4.2-gcj/bin/java
*+ 2           /usr/java/j2sdk1.4.2_08//bin/java
   3           /usr/java/jdk1.5.0_04/bin/java

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