1. 程式人生 > >Ubuntu (and Debian) have an elegant way to manage libraries like the jdk.

Ubuntu (and Debian) have an elegant way to manage libraries like the jdk.

Using update-alternatives you can manage multiple jdk libraries on the same system, choosing which one you want to use as the main one.

First you have to install an alternative for the new installed jdk:

sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.7.0_45/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.7.0_45/bin/javac" 1

In this way you install the new jdk as an alternative to the original one. Then you can choose which one you wan to use:

sudo update-alternatives --config java
sudo update-alternatives --config javac

You will be asked to choose which jdk you want to use, on my system I have:

There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                           Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-6-openjdk-i386/jre/bin/java   1061      auto mode
  1            /usr/lib/jvm/java-6-openjdk-i386/jre/bin/java   1061      manual mode
* 2            /usr/lib/jvm/java-7-openjdk-i386/jre/bin/java   1051      manual mode

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

At any time you can see what alternatives you have for java or javac using the --list option:

sudo update-alternatives --list java
sudo update-alternatives --list javac

To see more options check the update-alternatives man page.