Sunday, September 16, 2018

De-Mystifying and Installing Orace Java for Linux

There is always some confusion when it comes to installing Java for Linux. A lot of applications are available for Java, especially for business. However, most of the instructions that come with these applications are for installing Java for Windows, which may seem to make it seem that installing Java for Windows is easier. Installing Java for Linux is in fact no more harder. It's just that Java itself has so many options. 
First, there are three different editions of Java: 
  1. Java Standard Edition (SE)
  2. Java Enterprise Edition (EE)
  3. Javae Micro Edition (ME). 
Most of the time, when refering to Java during installation, the instructions are referring to Java SE (Java Standard Edition). 
Next, Java offers several packages for installation.There are two different Java SE packages
  1. Java Runtime Environment (JRE) or sometimes called Java SE Runtime Edition. JRE is an implementation of the Java Virtual Machine (JVM). The JVM is the component that runs compiled Java applications and applets.
  2. Java Development Kit (JDK). JDK includes the JRE (and JVM) and other software that is required for developing, and compiling Java applications and applets.
Certain applications require a specific version of JRE, for example JRE 8 or JRE 10, These application will mention which version it requires or get a hint from the installation instructions.
Finally, thereare also two different implementations of Java: OpenJDK and Oracle Java. OpenJDK is the reference implementation of Java and is fully open source. Oracle Java was the original reference implementation and is commonly used by commercial applications. It is not surprising that commercial application require Oracle Java instead of OpenJDK. Most Java applications will work fine with either implementation. However, use the version recommended by the application being installed. If the application refers to downloading Java from Oracle, then it's likely requires Oracle Java. 
Since most business application require Oracle Java, the instructions below are specifically for Oracle Java. To install OpenJDK, the steps are simpler since it can be installed directly using the yum command without downloading the installation file (Step 3). The steps are divided into several steps
  1. Get the download URL for the Oracle Java JRE (http://www.oracle.com/technetwork/java/javase/index.html). Accept the license.
  2. Download the JRE rpm file using the URL file (it should look something like this: http://download.oracle.com/otn-pub/java/jdk/8u162-b12/0da788060d494f5095bf8624735fa2f1/jre-8u162-linux-x64.rpm). This can be done using either wget or curl.
    1. Using wget:  wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u162-b12/0da788060d494f5095bf8624735fa2f1/jre-8u162-linux-x64.rpm"
    2. Using curl: curl -L -b "oraclelicense=a" -O http://download.oracle.com/otn-pub/java/jdk/8u162-b12/0da788060d494f5095bf8624735fa2f1/jre-8u162-linux-x64.rpm
  3. Install the RPM file using YUM: 
    sudo yum localinstall jre-8u161-linux-x64.rpm
  4. Confirm alternatives setting and take note of the path: sudo alternatives --config java
  5. Setup the environment variable for all users using the path from the step above (eg. /usr/java/jre1.8.0_161/jre):
    sudo sh -c "echo export JAVA_HOME=/usr/java/jre1.8.0_161/jre >> /etc/environment"  
Finally, setup the shell environment by adding the following commands to the /etc/bashrc file
export JAVA_HOME=/usr/java/jre1.8.0_161
export JRE_HOME=/usr/java/jre1.8.0_161/jre
export PATH=$PATH:/usr/java/jre1.8.0_161/bin:/usr/java/jre1.8.0_161/jre/bin
 
For extra points, the jar and jac Java components can also be setup to ensure a wider range of applications can use the installed java.
alternatives --install /usr/bin/jar jar /usr/java/jdk1.8.0_161/bin/jar 2
alternatives --install /usr/bin/javac javac /usr/java/jdk1.8.0_161/bin/javac 2
alternatives --set jar /opt/jdk1.8.0_161/bin/jar
alternatives --set javac /opt/jdk1.8.0_161/bin/javac