INSTALL JAVA 8 ON CENTOS 7

PREREQUISITES

update yum

$ sudo yum update

first make sure you have wget

$ sudo yum install wget

INSTALL JAVA 8

go to opt directory

$ cd /opt/

download latest version of java
see official page for latest version: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

$ sudo 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/8u101-b13/jdk-8u101-linux-x64.tar.gz”

extract it, it will create the directory jdk1.8.0_101

$ sudo tar zxf jdk-8u101-linux-x64.tar.gz

let’s see if any alternative old java used

$ cd /etc/alternatives
$ ls -l java
java -> /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.51-2.4.5.5.el7.x86_64/jre/bin/java

if your java points to an old version i.e 1.7.x then follow this steps

$ sudo alternatives –install /usr/bin/java java /opt/jdk1.8.0_101/bin/java 2
$ sudo alternatives –config java

You will see a list of jdks, select the latest java number (I selected number 2 which is the /opt/jdk1.8.0_101/bin/java

Now JAVA 8 has been successfully installed on the system. It’s also recommend to setup javac and jar commands path using alternatives

$ sudo alternatives –install /usr/bin/jar jar /opt/jdk1.8.0_101/bin/jar 2
$ sudo alternatives –install /usr/bin/javac javac /opt/jdk1.8.0_101/bin/javac 2
$ sudo alternatives –set jar /opt/jdk1.8.0_101/bin/jar
$ sudo alternatives –set javac /opt/jdk1.8.0_101/bin/javac

check java verion

$ java -version
java version “1.8.0_101”
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode

configure java environmental variables

 
SETUP ENVIRONMENTAL VARIABLES

setup JAVA_HOME variable

$ export JAVA_HOME=/opt/jdk1.8.0_101/

setup JRE_HOME

$ export JRE_HOME=/opt/jdk1.8.0_101/jre/

setup PATH variable

$ export PATH=$PATH:/opt/jdk1.8.0_101/bin:/opt/jdk1.8.0_101/jre/bin

However, to make it persistent use the below method instead of above

become root

$ sudo su

go to bashrc

$ vi /etc/bashrc
export JAVA_HOME=/opt/jdk1.8.0_101/
export JRE_HOME=/opt/jdk1.8.0_101/jre/
export PATH=$PATH:/opt/jdk1.8.0_101/bin:/opt/jdk1.8.0_101/jre/bin

source it

$ source /etc/bashrc