2.3. Setting the Default JDK with the alternatives Utility

alternatives is a tool for managing different software packages that provide the same functionality. Red Hat Enterprise Linux uses alternatives to ensure that only one Java Development Kit (JDK) is set as the system default at one time.

Important

Installing a JDK from the Red Hat Network will normally result in an automatically configured system. However, if multiple JDKs have been installed, it is possible that alternatives may contain conflicting configurations. Perform the following steps to verify that the everything is set up correctly.

Example 2.1. Using alternatives to Configure the Default Java Development Kit

[localhost]# /usr/sbin/alternatives --config java

There are 2 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
   1           /usr/lib/jvm/jre-1.4.2-gcj/bin/java
*+ 2           /usr/lib/jvm/jre-1.6.0-openjdk/bin/java

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

Procedure 2.3. Using alternatives to Set the Default JDK

  1. Set java

    Input this command: /usr/sbin/alternatives --config java
    Next, follow the on-screen directions to ensure that the correct version of java is selected. Table 2.1, “java alternative commands” shows the relevant command settings for each of the different JDKs.

    Table 2.1. java alternative commands

    JDK alternative command
    OpenJDK 1.6 /usr/lib/jvm/jre-1.6.0-openjdk/bin/java
    Sun Microsystems JDK 1.6 /usr/lib/jvm/jre-1.6.0-sun/bin/java

  2. Set javac

    Enter this command: /usr/sbin/alternatives --config javac
    Follow the on-screen directions to ensure that the correct version of javac is selected. Table 2.2, “javac alternative commands” shows the appropriate command settings for the different JDKs.

    Table 2.2. javac alternative commands

    JDK alternative command
    OpenJDK 1.6 /usr/lib/jvm/java-1.6.0-openjdk/bin/javac
    Sun Microsystems JDK 1.6 /usr/lib/jvm/java-1.6.0-sun/bin/javac

  3. Extra Step: Set java_sdk_1.6.0

    The Sun Microsystems JDK 1.6 requires an additional command be run: /usr/sbin/alternatives --config java_sdk_1.6.0
    Follow the on-screen directions to ensure that the correct java_sdk is selected. It is /usr/lib/jvm/java-1.6.0-sun.