I like (need) to have several JDKs installed on my system and I use Debian’s alternatives mechanism to switch easily between them. It’s just a clever use of symlinks. A nice aspect of the alternatives system is the possibility to connect “slaves”: links to other files which should be changed when their master changes. Since the JDK is made up of quite a few commands, here is what I use when I add a new version to my system:
sudo update-alternatives --install /usr/bin/java java $1/bin/java 200 \ --slave /usr/bin/jar jar $1/bin/jar \ --slave /usr/bin/jarsigner jarsigner $1/bin/jarsigner \ --slave /usr/bin/javac javac $1/bin/javac \ --slave /usr/bin/javadoc javadoc $1/bin/javadoc \ --slave /usr/bin/javah javah $1/bin/javah \ --slave /usr/bin/javap javap $1/bin/javap \ --slave /usr/bin/javaws javaws $1/bin/javaws
Then I just invoke the following command to select the JDK I want to use:
sudo update-alternatives --config java
I get presented with the following
There are 5 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/ibm-java-x86_64-60/bin/java 200 auto mode 1 /usr/lib/jvm/ibm-java-x86_64-60/bin/java 200 manual mode 2 /usr/lib/jvm/ibm-java-x86_64-70/bin/java 200 manual mode 3 /usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java 200 manual mode 4 /usr/lib/jvm/jdk1.6.0_31/bin/java 200 manual mode * 5 /usr/lib/jvm/jdk1.7.0_03/bin/java 200 manual mode Press enter to keep the current choice[*], or type selection number:
And when you need to dynamically set JAVA_HOME to the appropriate path, use the following command:
export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")