Install latest Java on RHEL /CentOS and Fedora

This article explains to you how to install and configure Java JDK’s latest version on CentOS /RHEL servers. Here We are going to install the latest Java JDK version on the CentOS Linux Operating system Below is my CentOS Server information.

 Operating System: CentOS Linux 8 (Core)
       CPE OS Name: cpe:/o:centos:centos:8
            Kernel: Linux 4.18.0-147.el8.x86_64
      Architecture: x86-64

Two Method to download and Install

Method(1) : Through yum repository

Method(2) : Through Linux Binaries

2: Download & Install Java JDK through Linux Binary

Download the latest  Java SE  Development Kit from its official Java vendor website. Below are the steps to extract download JDK files in a temporary folder in Linux. You have to download the tar archive file. We downloaded the JDK file in our temp folder. We will go into the /tmp folder and extract it there.

OpenJDK JDK binaries available for Linux on jdk.java.net as compressed zip or tar.gz archive.

# cd /tmp
# wget https://download.java.net/java/GA/jdk16.0.1/7147401fd7354114ac51ef3e1328291f/9/GPL/openjdk-16.0.1_linux-x64_bin.tar.gz
# tar xzf openjdk-16.0.1_linux-x64_bin.tar.gz
# mv jdk-16.0.1 /opt

Install  Java 16

Now we have got extracted folder of java. Alternatives command is the best way to install java. Go to the extracted folder of java and run like below command as showing.

# cd /opt/jdk16.0.1
# alternatives --install /usr/bin/java java /opt/jdk-16.0.1/bin/java 2 

Now we should set  jar and javac  path

# alternatives --install /usr/bin/jar jar /opt/jdk-16.0.1/bin/jar 2

# alternatives --install /usr/bin/javac javac /opt/jdk-16.0.1/bin/javac 2

# alternatives --set jar /opt/jdk-16.0.1/bin/jar

# alternatives --set javac /opt/jdk-16.0.1/bin/javac

Set  Java Environment Variables :

After the set of javac and JRE paths, we will set the environment variables in Linux. Many application needs to use the JAVA_Home or JRE_HOME environment variables for the java executable to use, even you can not see the java version without the environment variables set.

Set java home variable use below command.

export JAVA_HOME=/opt/jdk-16.0.1/

Path set  jre home variable use below command

export JRE_HOME=/opt/jdk-16.0.1/jre/

Setting the path variables use below command

export PATH=$PATH:/opt/jdk-16.0.1/bin:/opt/jdk-16.0.1/jre/bin

Verify Installed Java Version

Now check the version of installed Java on the Linux box through the below command

[[email protected] jdk-16.0.1]# java --version
 openjdk 16.0.1 2021-04-20
 OpenJDK Runtime Environment (build 16.0.1+9-24)
 OpenJDK 64-Bit Server VM (build 16.0.1+9-24, mixed mode, sharing)

Like the java command not running after setting the environment variables, so you can add the below lines in bashrc and profile files in the /etc directory.

JAVA_HOME=/opt/jdk-16.0.1/
export JRE_HOME=/opt/jdk-16.0.1/jre
 export PATH=$JAVA_HOME/bin:$PATH

Congratulations, Now we have installed, run, and configure java!

About Sachin Gupta

I am a professional freelance contributor and founder of tech transit. Love to write and lover of education, culture, and community. I have been using it, setting, supporting, and maintaining it since 2009.Linux rocks!