Installing Oracle Java SDK in CentOS 7

Posted by Jose Estudillo on October 14, 2014

Installing Oracle Java SDK in CentOS 7

Before starting I want to point out that all the commands required below must be executed as super user. By default CentOS doesn’t add the newly create users to the sudoers list, because of this, we will have to become super user using the command su that will keep the root session open.

Downloading the binaries

Removing OpenJDK

# run as super user, su/sudo
yum remove java-1.7.0-openjdk.x86_64

to know which version you have to remove type yum remove java- and then press tab for autocompletion. In CentOS 7, yum remove java also does the job.

Installing the Oracle JDK rpm

For this example I will assume that the files are downloaded in /tmp/

# run as super user, su/sudo
rpm -Uvh /tmp/jdk-7u71-linux-x64.rpm

By default this will be installed in /usr/java/jdk{version} using a symbolic links, this will be mapped to /usr/java/default that can be changed to a different SDK if required. To see the symbolic links, type the following in the shell:

ls -l /usr/java/default 

this will return /usr/java/default -> /usr/java/latest if we repeat the same operation, with the target of the symbolic link:

ls -l /usr/java/latest

It will show the actual directory where the JDK is installed /usr/java/latest -> /usr/java/jdk1.7.0_71. Changing this symbolic link would allow us to point to a different JDK without any extra effort.

I will do the same with the JDK 8 to have both versions on the server

# run as super user, su/sudo
rpm -Uvh /tmp/jdk-8u25-linux-x64.rpm

In CentOS there is no need to redefine the alternatives for the java binaries, because they are defined already based on the default path (/usr/java/default) so all the java commands will be available (java, jar, javaws, …).

Defining environment variables

Depending on the use, some users, just define the JAVA_HOME variable in their bash profile /home/{username}/.bashrc adding it the following line:

export JAVA_HOME=/usr/java/default

In my case I intent to use the machine as a server so I want it to be defined for all the profiles, to achieve this, I create an sh file in the folder /etc/profile.d/ where it will be always loaded on startup. This can be done with the following command:

echo "export JAVA_HOME=/usr/java/default" > /etc/profile.d/java.sh

Notice that this requires the OS to restart to be able to see the effects. Once this is done, you should be able see the value of JAVA_HOME with:

echo $JAVA_HOME

Now we have Oracle Java SDK installed on the system.