How To Install Java On Rocky Linux 8 / CentOS 8 / RHEL 8

0

The Java JDK (Java Development Kit) is a software development environment used for developing Java Applications. The JDK is a collection of programming tools, notably JRE (Java Runtime Environment), Java (Loader for Java Application), Javac (Compiler), Jar (Archiver), etc.

JDK or JRE

Application developers who are new to Java often confuse the Java Development Kit with the Java Runtime Environment. The JDK is a package of tools for developing Java applications, whereas the JRE is a package of tools for running Java applications.

OpenJDK or Oracle Java

OpenJDK is an open-source implementation of the Oracle Java SE platform edition. Oracle develops Oracle Java SE, whereas the OpenJDK is developed by Oracle Corporation, OpenJDK and Java Community, Red Hat, Azul Systems, IBM, Apple Inc, and SAP SE.

There is no technical difference between OpenJDK and Oracle JDK.

THIS DOCUMENT IS ALSO AVAILABLE FOR

Install Java on Rocky Linux 8 / CentOS 8 / RHEL 8

Install OpenJDK or Oracle Java as per your requirement.

You can have multiple versions of Java (OpenJDK and Oracle Java) on your system. But, you can have only one default version of Java.

Install OpenJDK

Installing OpenJDK is a pretty straightforward process in Rocky Linux 8 / CentOS 8 / RHEL 8.

OpenJDK is available from Red Hat Enterprise Linux 8 for x86_64 – AppStream (RPMs) in RHEL 8.

You can use the yum command to install OpenJDK.

Install OpenJDK JDK

### Java JDK 8 ###

dnf install -y java-1.8.0-openjdk-devel

### Java JDK 11 ###

dnf install -y java-11-openjdk-devel

Install OpenJDK JRE

### Java JRE 8 ###

yum install -y java-1.8.0-openjdk

### Java JRE 11 ###

yum install -y java-11-openjdk

Install Oracle Java

There is no separate JRE (Java Runtime Environment) anymore. Instead, Oracle JDK now provides JRE as well.

Download Oracle Java JDK

You can either use the command line or browser to download the JDK.

Go to the Oracle JDK page to download packages using the browser. Then, download the rpm binary package for easy installation.

Oracle Java JDK 16:

Download Oracle Java 16 (v16.0.1)

Oracle Java JDK 11 (LTS):

Download Oracle Java 11 (v11.0.11) (Login Required)

Oracle Java JDK 8:

Download Oracle Java 8 (v8u291) (Login Required)

If you still want to download through the command line, install the wget package.

dnf install -y wget

Then, use the below command to download Oracle Java using the terminal.

### Oracle Java JDK 12 ###

wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" https://download.oracle.com/otn-pub/java/jdk/16.0.1+9/7147401fd7354114ac51ef3e1328291f/jdk-16.0.1_linux-x64_bin.rpm

### Oracle Java JDK 11 ###

LOGIN REQUIRED

### Oracle Java JDK 8 ###

LOGIN REQUIRED

Install Oracle Java JDK

Install Oracle Java JDK using the rpm command.

### Oracle Java JDK 12 ###

rpm -ivh jdk-16.0.1_linux-x64_bin.rpm

### Oracle Java JDK 11 (LTS) ###

rpm -ivh jdk-11.0.11_linux-x64_bin.rpm

### Oracle Java JDK 8 ###

rpm -ivh jdk-8u291-linux-x64.rpm
By default, Java JDK is installed in /usr/java/ directory. To install Oracle JDK to a custom directory, use rpm -ivh –prefix=/<path>/ rpmfile command.

Set Default Java Version

Use the alternatives command to set the default java version.

alternatives --config java

Select Java:

If your system has multiple Java versions, the above command will list all Java versions like below.

There are 3 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
   1           java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.11.0.9-2.el8_4.x86_64/bin/java)
   2           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.292.b10-1.el8_4.x86_64/jre/bin/java)
*+ 3           /usr/java/jdk-16.0.1/bin/java

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

Enter the number below the selection column to set the default Java version.

Here, I chose 1 for OpenJDK 11.

Verify Java Version

Check the java version using the following command.

java -version

Output:

openjdk version "11.0.11" 2021-04-20 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.11+9-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.11+9-LTS, mixed mode, sharing)

The output may vary depending upon the package and the version you chose to be the default Java version.

Setup Environmental Variables

The most important part has come now. Java applications often require JAVA environment variables to be set in the system.

Create a new file under /etc/profile.d directory.

vi /etc/profile.d/java.sh

Set variables based on the Java location and version.

export PATH=$PATH:/usr/lib/jvm/java-11-openjdk-11.0.11.0.9-2.el8_4.x86_64/bin/
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.11.0.9-2.el8_4.x86_64/
export J2SDKDIR=/usr/lib/jvm/java-11-openjdk-11.0.11.0.9-2.el8_4.x86_64/

Load the environments into the current session.

source /etc/profile.d/java.sh
To set the environment variables for a particular user, place the above variables in ~/.bash_profile file.

Conclusion

I hope, this post helped you install Java on your EL 8 system. Java must run applications such as Tomcat, Gradle, ELK Stack, Graylog, Eclipse IDE, Hadoop, etc.

You might also like