Step-by-Step Guide to Installing Java on CentOS 8

Java is a free, open-source programming language widely used in the distributed environment of the internet. There are two primary implementations: Oracle Java and OpenJDK. While Oracle Java includes some commercial features, OpenJDK is the open-source implementation of the Java Platform.

This tutorial will guide you through installing OpenJDK 8, OpenJDK 11, and Oracle Java 8 on CentOS 8.

Requirements

  • A server running CentOS 8.
  • Root privileges configured on your system.

Install OpenJDK 11

To begin, search for available OpenJDK versions in the CentOS 8 repository with the following command:

dnf search jdk

Expected output:

Last metadata expiration check: 1:23:41 ago on Wednesday 18 December 2019 02:23:23 AM EST.

========================================================= Summary & Name Matched: jdk =========================================================
java-11-openjdk-demo.x86_64 : OpenJDK Demos 11
java-1.8.0-openjdk-demo.x86_64 : OpenJDK Demos 8
java-11-openjdk-jmods.x86_64 : JMods for OpenJDK 11
java-11-openjdk-src.x86_64 : OpenJDK Source Bundle 11
java-1.8.0-openjdk-src.x86_64 : OpenJDK Source Bundle 8
java-11-openjdk.x86_64 : OpenJDK Runtime Environment 11
copy-jdk-configs.noarch : JDKs configuration files copier
java-1.8.0-openjdk.x86_64 : OpenJDK Runtime Environment 8
java-11-openjdk-javadoc.x86_64 : OpenJDK 11 API documentation
java-1.8.0-openjdk-javadoc.noarch : OpenJDK 8 API documentation
java-11-openjdk-devel.x86_64 : OpenJDK Development Environment 11
java-1.8.0-openjdk-devel.x86_64 : OpenJDK Development Environment 8
java-11-openjdk-headless.x86_64 : OpenJDK Headless Runtime Environment 11
java-1.8.0-openjdk-accessibility.x86_64 : OpenJDK 8 accessibility connector
java-1.8.0-openjdk-headless.x86_64 : OpenJDK Headless Runtime Environment 8
java-11-openjdk-javadoc-zip.x86_64 : OpenJDK 11 API documentation compressed in a single archive
java-1.8.0-openjdk-javadoc-zip.noarch : OpenJDK 8 API documentation compressed in a single archive
============================================================ Summary Matched: jdk =============================================================
icedtea-web.noarch : Additional Java components for OpenJDK - Java browser plug-in and Web Start implementation

Install OpenJDK 11 by executing:

dnf install java-11-openjdk-devel.x86_64

Once the installation is complete, verify the installed Java version using:

java -version

The output should appear as follows:

openjdk version "11.0.5" 2019-10-15 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.5+10-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.5+10-LTS, mixed mode, sharing)

Install OpenJDK 8

Certain Java-based applications require OpenJDK 8. To install it, run the command:

dnf install java-1.8.0-openjdk-devel

Verify the installed version with:

java -version

Expected output:

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

Switch Between Java Versions

You can simultaneously install multiple Java versions and switch among them as needed. Use the following command to switch from OpenJDK 8 to OpenJDK 11:

alternatives --config java

You’ll be prompted to choose your preferred Java version:

There are 2 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
   1           java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.5.10-0.el8_0.x86_64/bin/java)
*+ 2           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el8_0.x86_64/jre/bin/java)

Enter to keep the current selection[+], or type selection number: 1

Enter “1” and press Enter to switch to Java 11. Confirm your choice by checking the active Java version:

java -version

Install Oracle Java 8

You’ll need to log into the Oracle website and visit the Oracle Java 8 JDK Downloads Page. Accept the license agreement and download the jdk-8u231-linux-x64.rpm file.

Install Oracle Java 8 with:

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

To verify the installation, run:

java -version

Expected outcome:

java version "1.8.0_231"
Java(TM) SE Runtime Environment (build 1.8.0_231-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode)

Conclusion

We have successfully installed different Java versions on CentOS 8. You’re now equipped to install your preferred Java version and switch between them as necessary.

FAQ

How can I check which version of Java is currently active?

You can check the active Java version by running the command java -version.

Can I have both OpenJDK 8 and OpenJDK 11 installed simultaneously?

Yes, you can have multiple versions installed and use the alternatives --config java command to switch between them.

Is OpenJDK free to use?

Yes, OpenJDK is open-source and free to use without commercial features.

Why would I choose Oracle Java over OpenJDK?

Oracle Java includes additional commercial features and support options that might be necessary for certain enterprise applications.