Technical Recipes

How to install OpenJDK 7 on CentOS

1. make sure the CentOS 6 system is fully up-to-date
    # yum update
2. checked the system for any other installed Java packages
    # rpm -qa | grep java
    Find the relevant package name, such as java-1.6.0-openjdk-, for 64 bit OS

3. Remove old Java package
    Remove the package with the specific name found from above command.
    # yum remove java-1.6.0-openjdk

4. Search for the latest OpenJDK package
    # yum search java | grep 'java-1.7'
    You get:
	java-1.7.0-openjdk.x86_64 : OpenJDK Runtime Environment
	java-1.7.0-openjdk-devel.x86_64 : OpenJDK Development Environment
	java-1.7.0-openjdk-demo.x86_64 : OpenJDK Demos
	java-1.7.0-openjdk-javadoc.noarch : OpenJDK API Documentation
	java-1.7.0-openjdk-src.x86_64 : OpenJDK Source Bundle
    Find the relevant package name, such as java-1.7.0-openjdk.x86_64, for 64 bit OS. The java-1.7.0-openjdk package contains just the Java Runtime Environment. If you want to develop Java programs then install the java-1.7.0-openjdk-devel package.
5. download and install prebuilt OpenJDK packages
    Download the package with the specific name found from above command.
    # yum install java-1.7.0-openjdk.x86_64
    The actual package name being downloaded is, for example,
    The actual package being installed is:
    java-1.7.0-openjdk.x86_64 1:

6. Swap between OpenJDK and Sun/Oracle Java JDK/JRE 6 and 7 versions java

	# alternatives --config java

7. Build symbolic link
    Now run the following command:
    # ls -al /usr/bin/java
    you can find the /usr/bin/java is always pointed to /etc/alternatives/java, and
    # ls -al /etc/alternatives/java
    the /etc/alternatves/java has been update automatically to link to
    /etc/alternatives/java -> /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
    It's better to create a symlink to the installation folder /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/, for instance: /usr/lib/jvm/latest/, and set the alternatives/java to the symbolic link. That way, you don't have to update your alternatives each time you upgrade your JDK, considering you can add the symlink path to your alternatives. Same goes for your JAVA_HOME variable and firefox plugin, you don't have to alter those later on either.
    # ln -s /usr/lib/jvm/jre-1.7.0-openjdk.x86_64 /usr/lib/jvm/latest
    # alternatives --set java /usr/lib/jvm/latest/bin/java