Linux and Oracle JDK: Add or Install

Although all distributions of Linux have access to a purely open source “Java” for application support and basic Java development (called OpenJDK with the IcedTea Java Compiler), we all know that there are certain pieces of the full JDK that are missing or have yet to be reverse engineered and release as purely open source. The OpenJDK functions nicely enough as a dependency for OpenOffice/LibreOffice and can even do a fair job of running Apache Tomcat if you choose, but performance can be spotty, garbage collection (memory management) needs work and it generally isn’t “officially” supported as a JRE for applications (or games) that require Java. That’s where this article comes in, whether you currently have OpenJDK installed and want to install Oracle’s JDK alongside or are doing a fresh install, we will show you how to correctly install the JDK using the new “/etc/alternatives” setup our modern Linux distributions are using.

Obtain the Right Package
First step as you might guess, is to hit up Oracle for the latest copy of the Oracle JDK (I recommend the full JDK even if you won’t be doing development so you get the full toolset install). At the time of this article, Java Version 7 Update 60 was the latest “main stream” Java version. Although Java Version 8 is in general release, most application vendors are still only supporting the v7 tree.

There are two flavors each for 32bit and 64bit Java, I will assume you are running 64bit, but you can easily substitute the package you download in any of our examples. If you are running any of the RPM based (CentOS or Redhat for example) distributions, certainly pull down the RPM package from the site. Any other version of Linux, pull down the plain TAR Gzip version.

Install… Kind Of…
So, “install” is kind of a stretch given that the RPM version simply expands the package into /usr/lib/java (and sometimes /usr/lib/java/latest) and the TAR Gzip version expands it to a directory in the current directory with the full version. Let’s assume we are working with Java 7 Update 60 and using the Gzip package. You would perform the following steps at the command line:
sudo tar zxvf jdk-7u60-linux-x64.tar.zxvf

This should then leave you with a directory called “jdk-7u60-x64” (of course your mileage may vary on the exact name). We are going to name it something easier for us and move it to a permanent home that makes sense, like so:
sudo mv jdk-7u60-linux-x64 java
sudo mv java /opt

Now let’s make sure everything works the way we expect.

What Versions Are Where?
So by default, we if have the OpenJDK installed already, you will see a /usr/java/latest/bin/java file that is linked to by /usr/lib/java. Issuing a version query at the command line will show you something like this:
java -version
java version "1.7.0_55"
OpenJDK Runtime Environment (IcedTea 2.4.7) (7u55-2.4.7-1ubuntu1)
OpenJDK 64-Bit Server VM (build 24.51-b03, mixed mode)

Notice that although we have installed Oracle JDK, the default java is still the OpenJDK version, so you can verify our new version works by executing the following:
/opt/java/bin/java -version
java version "1.7.0_60"
Java(TM) SE Runtime Environment (build 1.7.0_60-b19)
Java HotSpot(TM) 64-Bit Server VM (build 24.60-b09, mixed mode)

The subtle difference being the update version and the “IcedTea” compiler vs. the “HotSpot” compiler. The reason we are defaulting to the OpenJDK is that is how the /etc/alternatives are currently set, but we can fix that.

An Alternate Reality
We need to update our alternatives installation to set the default java version to the newly installed Oracle JDK. Easy enough, for RPM based distributions, we can update the system to use the ‘java’ provided by our Oracle install like this:
alternatives --install /usr/bin/java java /opt/java/bin/java 200000

for all other distributions, use this:
update-alternatives --install /usr/bin/java java /opt/java/bin/java 200000

If you now check your “/etc/alternatives/java” file, you will see it links “/etc/alternatives/java” with “/opt/java/bin/java” and executing a java version at the command prompt will give you the HotSpot compiler version of Oracle’s Java.

Now you can repeat that action for the Java Compiler (javac), the Java WebStart (javaws) and the Java Package Manager (jar) applications so they will use the Oracle JDK. That’s it!

Java Home
Finally, you will want to make sure all your applications and compilers know where your default java version base directory is, so in the “/etc/profile” directory, add the following line to the end:
export JAVA_HOME="/opt/java"

Final Thoughts
This whole process should take about two minutes longer than it takes to download the package from Oracle. It’s a little more complex than just a straight install, but Linux Distributions are trying to standardize on the location of applications and related binaries. Using the “/etc/alternatives” system allow each of them to make application pathing and versions consistent without breaking the distribution’s traditional file structure. If you have any issues or questions, let me know below!

Terrence T. Cox

A veteran of twenty years in Information Technology in a variety of roles. He has worked in development, security and infrastructure well before they merged into what we now call DevOps. He provides training in Linux, VMWare, DevOps (Ansible, Jenkins, etc) as well as containers and AWS topics.