How to install Java on CentOS


Install Java Runtime Environment 1.8.0

Step 1: download https://www.java.com/en/download/manual.jsp

For CentOS 32bits, use Linux RPM

wget "https://sdlc-esd.oracle.com/ESD6/JSCDL/jdk/8u191-b12/2787e4a523244c269598db4e85c51e0c/jre-8u191-linux-i586.rpm?GroupName=JSC&FilePath=/ESD6/JSCDL/jdk/8u191-b12/2787e4a523244c269598db4e85c51e0c/jre-8u191-linux-i586.rpm&BHost=javadl.sun.com&File=jre-8u191-linux-i586.rpm&AuthParam=1546769589_67418eadf0052d11df6c6f17b4703a16&ext=.rpm" -O jre-8u191-linux-i586.rpm

For CentOS 64bits, use Linux x64 RPM

mkdir /usr/java
cd /usr/java
wget http://javadl.oracle.com/webapps/download/AutoDL?BundleId=230531_2f38c3b165be4555a1fa6e98c45e0808 -O jre-8u161-linux-x64.rpm

Result:

[root@tutorialspots ~]# mkdir /usr/java
[root@tutorialspots ~]# cd /usr/java
[root@tutorialspots java]# wget http://javadl.oracle.com/webapps/download/AutoDL?Bund
leId=230531_2f38c3b165be4555a1fa6e98c45e0808 -O jre-8u161-linux-x64.rpm
--2018-02-10 19:25:50--  http://javadl.oracle.com/webapps/download/AutoDL?Bundle
Id=230531_2f38c3b165be4555a1fa6e98c45e0808
Resolving javadl.oracle.com (javadl.oracle.com)... 137.254.120.23
Connecting to javadl.oracle.com (javadl.oracle.com)|137.254.120.23|:80... connec
ted.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: http://sdlc-esd.oracle.com/ESD6/JSCDL/jdk/8u161-b12/2f38c3b165be4555a1
fa6e98c45e0808/jre-8u161-linux-x64.rpm?GroupName=JSC&FilePath=/ESD6/JSCDL/jdk/8u
161-b12/2f38c3b165be4555a1fa6e98c45e0808/jre-8u161-linux-x64.rpm&BHost=javadl.su
n.com&File=jre-8u161-linux-x64.rpm&AuthParam=1518291951_e3d3d15ac294626fc62299b7
9dadbf66&ext=.rpm [following]
--2018-02-10 19:25:51--  http://sdlc-esd.oracle.com/ESD6/JSCDL/jdk/8u161-b12/2f3
8c3b165be4555a1fa6e98c45e0808/jre-8u161-linux-x64.rpm?GroupName=JSC&FilePath=/ES
D6/JSCDL/jdk/8u161-b12/2f38c3b165be4555a1fa6e98c45e0808/jre-8u161-linux-x64.rpm&
BHost=javadl.sun.com&File=jre-8u161-linux-x64.rpm&AuthParam=1518291951_e3d3d15ac
294626fc62299b79dadbf66&ext=.rpm
Resolving sdlc-esd.oracle.com (sdlc-esd.oracle.com)... 88.221.144.80, 88.221.144
.40
Connecting to sdlc-esd.oracle.com (sdlc-esd.oracle.com)|88.221.144.80|:80... con
nected.
HTTP request sent, awaiting response... 200 OK
Length: 63333908 (60M) [application/x-sdlc]
Saving to: `jre-8u161-linux-x64.rpm'

100%[======================================>] 63,333,908  10.2MB/s   in 6.7s

2018-02-10 19:25:58 (9.06 MB/s) - `jre-8u161-linux-x64.rpm' saved [63333908/6333
3908]

Step 2: install:
32bits

rpm -ivh jre-8u191-linux-i586.rpm

64bits

rpm -ivh jre-8u161-linux-x64.rpm

Result:

[root@tutorialspots java]# rpm -ivh jre-8u161-linux-x64.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:jre1.8-1.8.0_161-fcs             ################################# [100%]
Unpacking JAR files...
        plugin.jar...
        javaws.jar...
        deploy.jar...
        rt.jar...
        jsse.jar...
        charsets.jar...
        localedata.jar...

You should delete RPM file after the installation:

rm -f jre-8u161-linux-x64.rpm

Step 3: create file /etc/profile.d/java.sh with these lines:

export JAVA_HOME=/usr/java/jre1.8.0_161
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar

By using this command:

vi /etc/profile.d/java.sh

Step 4:

export JAVA_HOME=/usr/java/jre1.8.0_161
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar

Check:

[root@tutorialspots ~]# java -version
java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)

Install java-1.8.0-openjdk-headless or java-1.8.0-openjdk
Change step 1 and 2:

yum install java-1.8.0-openjdk-headless

or

yum install java-1.8.0-openjdk

result:

[root@tutorialspots ~]# yum install java-1.8.0-openjdk
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: repos-tx.psychz.net
 * epel: kdeforge2.unl.edu
 * extras: mirror.genesisadaptive.com
 * ius: dfw.mirror.rackspace.com
 * nux-dextop: mirror.li.nux.ro
 * remi-safe: mirror.bebout.net
 * updates: mirror.mobap.edu
Resolving Dependencies
--> Running transaction check
---> Package java-1.8.0-openjdk.x86_64 1:1.8.0.191.b12-1.el7_6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package               Arch      Version                       Repository  Size
================================================================================
Installing:
 java-1.8.0-openjdk    x86_64    1:1.8.0.191.b12-1.el7_6       updates    254 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 254 k
Installed size: 501 k
Is this ok [y/d/N]: y
Downloading packages:
java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64.rpm        | 254 kB   00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64            1/1
  Verifying  : 1:java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64            1/1

Installed:
  java-1.8.0-openjdk.x86_64 1:1.8.0.191.b12-1.el7_6

Complete!

Change step 3 and 4:
CentOS 7.6 x86_64

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64/jre
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib

Centos 8.3 x86_64

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.275.b01-1.el8_3.x86_64/jre
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib

Check:

[root@tutorialspots ~]# java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)