Installing Java 1.8 on Centos

21 May 2016

We are moving to enterprise Linux from Ubuntu to Red Hat, finally a dream come true for me being able to have some enterprise nix experience. To prepare myself, I removed my ElementaryOS and installed CentOS. If I have enough cash I'll probably get one of those Linux Developer Suite.

As an exercise, I'm installing Java 1.8 and hopefully install more dockers to my CentOS in the near future. A quick Google took me to techadmin's blog.

Downloading java 1.8

$ cd /opt/
$ 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/8u91-b14/jdk-8u91-linux-x64.tar.gz"
$ tar xzf jdk-8u91-linux-x64.tar.gz

Install using alternatives

$ cd /opt/jdk1.8.0_91/
$ alternatives --install /usr/bin/java java /opt/jdk1.8.0_91/bin/java 2
$ alternatives --config java

There are 3 programs that provides 'java'.

  Selection    Command
-----------------------------------------------
   1           /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.101-2.6.6.1.el7_2.x86_64/jre/bin/java
*+ 2           /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.91-0.b14.el7_2.x86_64/jre/bin/java
   3           /opt/jdk1.8.0_91/bin/java

Enter to keep the current selection[+], or type selection number: 3

Installing javac and jar as well.

$ alternatives --install /usr/bin/jar jar /opt/jdk1.8.0_91/bin/jar 2
$ alternatives --install /usr/bin/javac javac /opt/jdk1.8.0_91/bin/javac 2
$ alternatives --set jar /opt/jdk1.8.0_91/bin/jar
$ alternatives --set javac /opt/jdk1.8.0_91/bin/javac

Setting up environment variables

The following steps are optional.

export JAVA_HOME=/opt/jdk1.8.0_91
export JRE_HOME=/opt/jdk1.8.0_91/jre
export PATH=$PATH:/opt/jdk1.8.0_91/bin:/opt/jdk1.8.0_91/jre/bin

Verifying your version of java

$ java -version
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)