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 Stream /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 Stream 8
       CPE OS Name: cpe:/o:centos:centos:8
            Kernel: Linux 4.18.0-358.el8.x86_64
      Architecture: x86-64

Two Method to download and Install

1 : Through yum repository

2: Through Linux Binaries

Method 1: Install Java through yum utility

Now Java RPM package is available on the app stream repository.

# yum search openjdk 

Now list and install the latest version of Java install.

# yum list java-17-openjdk 
# yum install java-17-openjdk

Method 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 are available for Linux on jdk.java.net as compressed zip or tar.gz archives.

# cd /tmp
# wget https://download.java.net/java/GA/jdk18/43f95e8614114aeaa8e8a5fcf20a682d/36/GPL/openjdk-18_linux-x64_bin.tar.gz
# tar xzf openjdk-18_linux-x64_bin.tar.gz
# mv jdk-18  /opt

Install  Java 18

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 shown.

# cd /opt/jdk-18

# alternatives --install /usr/bin/java java /opt/jdk-18/bin/java 2 

Now we should set the jar and javac  path

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

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

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

# alternatives --set javac /opt/jdk-18/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.

Set java home variable use below command.

export JAVA_HOME=/opt/jdk-18

Path set  jre home variable use below command

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

Setting the path variables use below command

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

Verify Installed Java Version

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

[[email protected]]# java --version
openjdk 18 2022-03-22
OpenJDK Runtime Environment (build 18+36-2087)
OpenJDK 64-Bit Server VM (build 18+36-2087, mixed mode, sharing)

if 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-18/
export JRE_HOME=/opt/jdk-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!