Install Java 8 on CentOS/RHEL 7.x

If you have a fresh installation, it is recommended to run the update first
yum update
Usually Java comes installed on CentOS 7 (Everything), for CentOS 7 minimal you may need to install Java for various setups. On a CentOS 7 Everything, you can verify it by simply checking the version:
java -version

The output:

# java -version
openjdk version "1.8.0_111"
OpenJDK Runtime Environment (build 1.8.0_111-b15)
OpenJDK 64-Bit Server VM (build 25.111-b15, mixed mode)
Java 1.8.0_111-b15 is the latest available, ‘1.8.0’ states it is Java 8, ‘u111’ means update 111, and ‘b15’means build 15.
For CentOS 7 minimal, the same command will give a different output:
# java -version
-bash: java: command not found
Which means Java is not installed. The latest Java version currently is Java 8.


Installing Java 8 using yum

It’ll install the latest Java 8 update i.e. Java 1.8.0_111-b15, where ‘1.8.0’ states it is Java 8, ‘u111’ means update 111, and ‘b15’means build 15.
yum install java-1.8.0-openjdk
The exact output:
# yum install java-1.8.0-openjdk
Installed:
java-1.8.0-openjdk.x86_64 1:1.8.0.111-1.b15.el7_2Dependency Installed:
java-1.8.0-openjdk-headless.x86_64 1:1.8.0.111-1.b15.el7_2     javapackages-tools.noarch 0:3.4.1-11.el7     lksctp-tools.x86_64 0:1.0.13-3.el7     python-javapackages.noarch 0:3.4.1-11.el7
python-lxml.x86_64 0:3.2.1-4.el7                               ttmkfdir.x86_64 0:3.0.9-42.el7               tzdata-java.noarch 0:2016h-1.el7       xorg-x11-font-utils.x86_64 1:7.5-20.el7
xorg-x11-fonts-Type1.noarch 0:7.5-9.el7Complete!]

Verify if Java has been installed

[nahmed@localhost ~]# java -version
openjdk version "1.8.0_111"
OpenJDK Runtime Environment (build 1.8.0_111-b15)
OpenJDK 64-Bit Server VM (build 25.111-b15, mixed mode)

Installing Java 8 manually

Download the latest Java 8

Use the following command to download the oracle java 8 rpm.
For 32 bit
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/8u111-b14/jdk-8u111-linux-i586.rpm"
For 64 bit
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/8u111-b14/jdk-8u111-linux-x64.rpm"
The exact output:
# cd /opt/
[nahmed@localhost 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/8u111-b14/jdk-8u111-linux-x64.rpm"
--2016-11-30 06:12:59--  http://download.oracle.com/otn-pub/java/jdk/8u111-b14/jdk-8u111-linux-x64.rpm
Resolving download.oracle.com (download.oracle.com)... 23.61.195.121, 23.61.195.144
Connecting to download.oracle.com (download.oracle.com)|23.61.195.121|:80... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://edelivery.oracle.com/otn-pub/java/jdk/8u111-b14/jdk-8u111-linux-x64.rpm [following]
--2016-11-30 06:12:59--  https://edelivery.oracle.com/otn-pub/java/jdk/8u111-b14/jdk-8u111-linux-x64.rpm
Resolving edelivery.oracle.com (edelivery.oracle.com)... 23.54.248.20, 2600:1406:1a:394::2d3e, 2600:1406:1a:3a1::2d3e
Connecting to edelivery.oracle.com (edelivery.oracle.com)|23.54.248.20|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: http://download.oracle.com/otn-pub/java/jdk/8u111-b14/jdk-8u111-linux-x64.rpm?AuthParam=1480515045_b6ade557f29a64a4278bdec5aa1958a8 [following]
--2016-11-30 06:12:59--  http://download.oracle.com/otn-pub/java/jdk/8u111-b14/jdk-8u111-linux-x64.rpm?AuthParam=1480515045_b6ade557f29a64a4278bdec5aa1958a8
Connecting to download.oracle.com (download.oracle.com)|23.61.195.121|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 166040563 (158M) [application/x-redhat-package-manager]
Saving to: ‘jdk-8u111-linux-x64.rpm’100%[========================================================================================================================================================>] 166,040,563 14.1MB/s   in 12s2016-11-30 06:13:12 (12.7 MB/s) - ‘jdk-8u111-linux-x64.rpm’ saved [166040563/166040563]

Installing Java 8 rpm

Use the below command to Install Oracle Java 8 (jdk-8u60) on your system using RPM file.
For 64 bit
# rpm -ivh jdk-8u111-linux-x64.rpm
For 32 bit
# rpm -ivh jdk-8u111-linux-i586.rpm
The exact output:
[nahmed@localhost opt]# ls
jdk-8u111-linux-x64.rpm
[nahmed@localhost opt]# rpm -ivh jdk-8u111-linux-x64.rpm
Preparing...                          ################################# [100%]
Updating / installing...
1:jdk1.8.0_111-2000:1.8.0_111-fcs  ################################# [100%]
Unpacking JAR files...
tools.jar...
plugin.jar...
javaws.jar...
deploy.jar...
rt.jar...
jsse.jar...
charsets.jar...
localedata.jar...
Verifying the Java version
After finishing installation check Java version using below command:
[nahmed@localhost opt]# java -version
java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)

Multiple Java versions

Usually you may have more than one version of Java installed, to choose between the versions:
alternatives --config java
The exact output:
[nahmed@localhost]# alternatives --config java
There are 2 programs which provide 'java'.Selection    Command
-----------------------------------------------
1           /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.111-2.6.7.2.el7_2.x86_64/jre/bin/java
*+ 2           /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.111-1.b15.el7_2.x86_64/jre/bin/javaEnter to keep the current selection[+], or type selection number:
Just type the number 1 (Java 7) or 2 (Java 8) and hit enter.
In case you just want to keep a single version of Java, remove the rest i.e. to remove the Java 7:
yum remove java-1.7.0-openjdk

Leave a Reply

Your email address will not be published. Required fields are marked *