How to Install Java on CentOS 8

Updated on

4 min read

Install Java on CentOS 8

Java is one of the most popular programming languages used to build different types of applications and systems.

There are two different implementations of Java, OpenJDK and Oracle Java, with almost no differences between them, except that Oracle Java has a few additional commercial features. Oracle Java License permits only non-commercial use of the software, such as personal use and development use. OpenJDK is an open-source implementation of the Java Platform.

The default CentOS 8 repositories include the latest two major Java LTS versions, Java 8 and Java 11.

In this tutorial, we will explain how to install one or more Java (OpenJDK) versions on CentOS 8 and how to set the default Java via alternatives.

Installing OpenJDK 11

The general recommendation is to install the latest Java LTS version (JDK 11) version. Some Java-based applications may require a specific version of Java, so you might need to consult the application documentation.

To install the OpenJDK 11 on CentOS 8, run the following command as root or user with sudo privileges :

sudo dnf install java-11-openjdk-devel

Once the installation is complete, you can verify it by checking the Java version:

java -version

The output should look something like this:

openjdk version "11.0.4" 2019-07-16 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.4+11-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.4+11-LTS, mixed mode, sharing)

That’s it! You have successfully installed Java on your CentOS 8 system.

CentOS 8 also supports a headless version of OpenJDK that provides a minimal Java runtime needed for executing applications without a graphical user interface (no keyboard, mouse, and display systems support). This version is more suitable for server applications since it has fewer dependencies and uses fewer system resources.

To install only the headless OpenJDK 11 type:

sudo dnf install java-11-openjdk-headless

If you already installed java-11-openjdk-devel, the headless package will be installed as a dependency.

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 dnf install java-1.8.0-openjdk-devel

Verify the installation by checking the Java version:

java -version

The output should look something like this:

openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)

For minimal Java runtime, install the java-1.8.0-openjdk-headless package.

Setting the Default Java Version

If you installed multiple Java versions on your CentOS system, you can use the alternatives system to set which Java version will be used when you type java in the terminal.

To check what Java version is set as the default one, type:

java -version

If you want to change the default version, use the alternatives command:

sudo alternatives --config java

The output will look something like below:

There are 2 programs which provide 'java'.

  Selection    Command
   1           java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-
*+ 2           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-

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

A list of all installed Java versions will be printed on the screen. Enter the number of the version you want to use as the default and press Enter.

You may also want to change the default javac version:

sudo alternatives --config java

javac is a command utility for compiling Java programs.

Setting the JAVA_HOME Environment Variable

The JAVA_HOME environment variable is used by some Java applications to determine the Java installation location and specify which Java version should be used to run the application.

To set the JAVA_HOME variable on a per-user basis, 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 add the following line, at the end of the file:


For changes to take effect on your current shell, you can either log out and log in or use the source command:

source /etc/profile.d/

Verify that the JAVA_HOME environment variable was correctly set:


The output should show the path to the Java installation:


You can also set the JAVA_HOME in the application configuration, systemd unit file, or on the command line when launching the program.

For example, to run Maven using Java 8 you would type:

JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk mvn --version
Java version: 1.8.0_222, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-


CentOS 8 supports two major Java versions, Java 8 and Java 11, which can be installed using the dnf package manager.

If you have any questions, feel free to leave a comment.