3.3. Install OpenJDK on Red Hat Enterprise Linux

Introduction

OpenJDK is one of many Java Development Kits (JDKs) supported in Red Hat Enterprise Linux for use with JBoss enterprise products. This task shows you how to install OpenJDK in Red Hat Enterprise Linux, and how to configure your system to use it as the default JDK.

Note

It is possible to install multiple JDKs on a single system.
Task Prerequisites

You must meet the following conditions before continuing with this task:

  • You must be running Red Hat Enterprise Linux 6. OpenJDK is not available or supported for Red Hat Enterprise Linux 5 at this time.
  • The server running Red Hat Enterprise Linux 6 must be registered with RHN and subscribed to the base channel. Refer to the Package Management section of the Red Hat Enterprise Linux 6 Deployment Guide on https://access.redhat.com/knowledge/docs/ for more information about managing subscriptions and entitlements on Red Hat Enterprise Linux systems.
  1. Install the OpenJDK RPM.

    There are two different ways to install an RPM, depending on whether you have used a command-line interface (CLI) or a Graphical User Interface (GUI).
    • From the CLI

      After logging in with administrator access, run one of the following two commands:
      OpenJDK 6
      yum install java-1.6.0-openjdk-devel
      OpenJDK 7
      yum install java-1.7.0-openjdk-devel
    • From a GUI

      1. In Gnome, select Administration from the System menu, and click Add / Remove Software.
      2. Search for openjdk and choose the java-1.6.0-openjdk-devel option for OpenJDK 6 or the java-1.7.0-openjdk-devel option for OpenJDK 7.
      3. Click Apply to install the selected packages and dependencies.
      4. When prompted, enter the administrator password to install the packages.
  2. Optional: Set the JAVA_HOME environment variable.

    Some applications such as Apache Maven and Apache Ant require you to set the JAVA_HOME environment variable. If you need to do this, follow these steps.
    1. Determine the correct value for JAVA_HOME. Red Hat Enterprise Linux installs OpenJDK 1.6 into either /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/ or /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/, depending on whether your system is a 32-bit or 64-bit architecture. The JAVA_HOME should point to the directory containing a bin/java executable.
    2. As the user who will use OpenJDK, open the shell configuration file. For the Bash shell, this file is /home/username/.bashrc.
    3. At the bottom of the file, type the following line, replacing the hypothetical path with the actual path to use on your own system: export JAVA_HOME="/path/to/java/home"
    4. Save the file, and log out of and back into your session.
  3. Switch between different OpenJDK versions using the alternatives utility.

    Red Hat Enterprise Linux includes a utility called alternatives, which lets you change the default version for applications which allow multiple versions to be installed. OpenJDK is one such application.
    To use the alternatives utility, perform the following steps. Note that setting the environment variables overrides the behavior of the alternatives command. For instance, if you use a script which manually sets $JAVA_HOME and $JAVA variables to a different JDK from the one specified by the alternatives command, the environment variables will override the command.
    • Log in as the root user, or prefix the commands below with the sudo command.
    • The alternatives command is not installed by default, but may already be installed on your system. If not, install the alternatives package by running the following command:
      yum install alternatives
    • Issue the following command:
      /usr/sbin/alternatives --config java
    • Follow the prompts that follow to set the default version of OpenJDK.
Result:

OpenJDK is installed on your server and available for your use. If necessarily, the JAVA_HOME environment variable has been specified as well. If necessary, the default OpenJDK for your system has been set using the alternatives utility.