How To Install Oracle Java JDK 11 / 8 on CentOS 7 / RHEL 7

1

Today we will be looking into the installation of Oracle Java JDK 11 / 8 on RHEL 7 / CentOS 7. Java JDK is the most important base for the developers.

This guide will be more useful for them to set up a development environment when they especially use RHEL 7 or CentOS 7.

Install the wget package.

yum install -y wget

Download Java SDK

You can either use command line or browser to download the JDK, better to use the browser, the easiest one.

Visit the Oracle SDK page to download.

Oracle Java JDK 11: (Current)

Download Oracle JDK 11

Oracle Java JDK 8: (End of Public Updates – Jan 2019)

Download Oracle JDK 8

If you still want to use the command line, use the below command.

### Oracle Java JDK 11 ###

wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" https://download.oracle.com/otn-pub/java/jdk/11.0.1+13/90cf5d8f270a4347a95050320eef3fb7/jdk-11.0.1_linux-x64_bin.tar.gz

### Oracle Java JDK 8 ###

wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" https://download.oracle.com/otn-pub/java/jdk/8u191-b12/2787e4a523244c269598db4e85c51e0c/jdk-8u191-linux-x64.tar.gz

Extract the downloaded JDK archive to the desired directory (Ex. /usr/)

tar -zxvf jdk-*

mv jdk* /usr/

Install Oracle Java 8

Once moved the java to the desired location, you must setup Java with the alternatives commands. Below command will install the Java on your system.

### Oracle Java JDK 11 ###

update-alternatives --install /usr/bin/java java /usr/jdk-11.*/bin/java 2

### Oracle Java JDK 8 ###

update-alternatives --install /usr/bin/java java /usr/jdk1.8.*/bin/java 2

Set the default java using the below command.

alternatives --config java

Select the latest Java:

If your system has multiple versions of Java installed, then the above command may list you all, like below.

There are 2 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
*+ 1           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64/jre/bin/java)
   2           /usr/jdk-11.0.1/bin/java

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

Verify the Java

Once you set up the alternatives. Check the java version installed using the following command.

java -version

Output:

Oracle JDK 11:

java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)

Oracle JDK 8:

java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

Setup Environmental Variable

The most important part has come now, it should be set before the installation of any Java-based applications.

To set JAVA environment variables, create a new file under /etc/profile.d directory.

vi /etc/profile.d/javajdk.sh

Place the variables based on the JDK location and version.

Oracle JDK 11:

export PATH=$PATH:/usr/jdk-11.0.1/bin
export JAVA_HOME=/usr/jdk-11.0.1
export J2SDKDIR=/usr/jdk-11.0.1

Oracle JDK 8:

export PATH=$PATH:/usr/jdk1.8.0_191/bin
export JAVA_HOME=/usr/jdk1.8.0_191
export JRE_HOME=/usr/jdk1.8.0_191/jre/
export J2SDKDIR=/usr/jdk1.8.0_191
export J2REDIR=/usr/jdk1.8.0_191/jre

Load the environments into the current session.

source /etc/profile.d/javajdk.sh

To set the environment variables permanent for a particular user, place the above in ~/.bash_profile file.

That’s All.

Further Reading

You might also like

How To Install Oracle Java JDK 11 / 8 on CentOS 7 / RHEL 7

1