How to Install Java on CentOS 8

In this article, We are going to perform How to Install JAVA on CentOS 8. Here we are installing OpenJDK 11 and 8 stable version on CentOS 8.

Introduction

OpenJDK is free and open-source implementation of the Java SE Platform.

Prerequisites

  • CentOS 8 with Minimal Installation
  • SSH access with sudo privileges

Install Java on CentOS 8

Here We are going to install OpenJDK 11 and OpenJDK 8 stable version on CentOS 8

How to Install OpenJDK 11 on CentOS 8

Lets install OpenJDK 11 on CentOS 8 using below commands.

$ sudo yum update

$ sudo dnf install java-11-openjdk

Lets verify java version

$ java -version

Output:

openjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10)
OpenJDK 64-Bit Server VM (build 11.0.7+10, mixed mode, sharing)

How to Install OpenJDK 8 on CentOS 8

Lets install OpenJDK 8 on CentOS 8 using below commands.

$ sudo yum update

$ sudo dnf install java-1.8.0-openjdk

Lets verify java version

$ java -version

Output:

openjdk version "1.8.0_252"
OpenJDK Runtime Environment (build 1.8.0_252)
OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)

Set Default Java Version using update alternatives

If you have installed multiple Java versions on your system and you want to set Default Java version on CentOS 8. Lets set Default Java Version using update alternatives command,

$ sudo update-alternatives --config java

It prompts for selecting by 0,1,2..so choose accordingly:

sudo update-alternatives --config java
There are 3 choices for the alternative java (providing /usr/bin/java).

Selection Path Priority Status
------------------------------------------------------------
   0 /usr/lib/jvm/zulu-11-amd64/bin/java 1113900 auto mode
   1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode
* 2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode
   3 /usr/lib/jvm/zulu-11-amd64/bin/java 1113900 manual mode

Press <enter> to keep the current choice[*], or type selection number: 1

To verify the if java version switched

$ java -version

Setting the JAVA_HOME Environment Variables

To define the environment variable open the below file:
$ sudo nano /etc/profile.d/java.sh
Paste the below variable into the file:

For OpenJDK 8 java is installed at /usr/lib/jvm/java-11-openjdk-amd64/bin/java location

For OpenJDK 11 Java is installed at /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java location

/etc/profile.d/java.sh
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"

Load the environment variable using below command

$ source /etc/profile.d/java.sh

Lets verify JAVA_HOME variable

$ echo $JAVA_HOME

Output:

/usr/lib/jvm/java-11-openjdk-amd64

If you seeing output like above then we have successfully set up the Java Home on CentOS 8.

Conclusion

In this article, We are going to perform How to Install JAVA on CentOS 8, configured JAVA_HOME, verified installed version.

Related Articles:

Install Zulu OpenJDK Version 11 on Ubuntu 20.04/18.04/16.04 LTS

Aniruddha Kanade

I am Shivdas Kanade working as Senior Site Reliability Engineer (Cloud and DevOps ). Believes in Sharing Knowledge.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link