How to install Java on CentOS 8

Java is a free, open source and one of the most popular programming languages explicitly designed for use in the distributed environment of the internet. There are two different implementations of Java, Oracle Java and OpenJDK. Oracle Java has some additional commercial features while OpenJDK is an open source implementation of the Java Platform.
In this tutorial, we will show you how to install OpenJDK 8, OpenJDK 11 and Oracle 8 on CentOS 8

1.Install OpenJDK 11

First, search for available versions of OpenJDK in the CentOS 8 repository by running the following command:

dnf search jdk

You will see the following output:

Now, install OpenJDK 11 with the following command:

dnf install java-11-openjdk-devel.x86_64

Once the installation is complete, you can verify the installed Java version by running the following command:

java -version

You will see the following output:

2. Install OpenJDK 8

Some Java-based applications are only supported on OpenJDK 8. In this case, you will need to install OpenJDK 8 on your server. You can install OpenJDK 8 easily with the following command:

dnf install java-1.8.0-openjdk-devel

Once the installation is complete, you can verify the installed Java version by running the following command:

java -version

You will see the following output:

3. Switch between Java versions

You can also install multiple Java versions in one system and switch between them as per your requirement.

You can run the following command to switch Java version from OpenJDK 8 to OpenJDK 11 as shown below:

alternatives --config java

You will be asked to provide your desired Java version as shown below:

Enter to keep the current selection[+], or type selection number: 1
Type 1 and press Enter to switch the Java version.

After changing again, check the current working Java version with the following command:

java -version

4.Install Oracle Java 8

First you will need to login to the Oracle Java website and go to the Oracle 8 JDK Download Page. Next, accept the license agreement and download the jdk-8u231-linux-x64.rpm package.

Once the download is complete, run the following command to install Oracle OpenJDK 8:

dnf localinstall jdk-8u231-linux-x64.rpm

After installing Oracle OpenJDK 8, you can verify the Java version with the following command:

java -version