Java is a general-purpose computer programming language that is concurrent, class-based, object-oriented, and specifically designed to have as few implementation dependencies as possible.

Java Development Kit (JDK)
This is an implementation of either one of the Java Platform, Standard Edition, Java Platform, Enterprise Edition, or Java Platform, Micro Edition platforms released by Oracle Corporation in the form of a binary product aimed at Java developers on Solaris, Linux, macOS or Windows.

In this guide, we will explain how to install the Java on a CentOS 8 server.

Installing OpenJDK 11
To install the OpenJDK 11 on CentOS 8, issue the following command:

sudo yum install java-11-openjdk-devel

Installing OpenJDK 8
Java 8, the previous Java LTS version, is still supported and widely used. If your application requires Java 8, you can install it by typing the following command:

sudo yum install java-1.8.0-openjdk-devel

Configuring the Default Java Version
From the above, we installed multiple versions of Java on our system. To set one as our default (i.e. the one that will run when a user runs the java command as some applications require certain environment variables to be set to locate which installation of Java to use.

Using Alternatives
The alternatives command, which manages default commands through symbolic links, can be used to select the default Java command.

To show the programs that provide the java command that is managed by alternatives, issue the command below:

sudo alternatives --config java

From the above, it asks you Enter to keep the current selection[+], or type selection number: to select another version.

Using Environment Variables
Numerous Java applications use the JAVA_HOME or JRE_HOME environment variables to determine which java executable to use. To configure the JAVA_HOME variable on a per-user basis, simply add it to the ~/.bashrc or any other configuration file which is loaded when the user logs in. For a system-wide setting, use a script inside the /etc/profile.d directory.

Assuming you want to set JAVA_HOME to OpenJDK 8, open the following file:

sudo nano /etc/profile.d/java.sh

Enter the following:

JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk"

Next, issue the command below:

source /etc/profile.d/java.sh

Lastly, verify that the JAVA_HOME environment variable was correctly set by issuing the command below:

echo $JAVA_HOME