java

How to install Java OpenJDK on CentOS 8

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

There are two different implementations of Java, OpenJDK and Oracle Java, the difference between them is the few additional commercial features on Oracle java.

CentOS 8’s repository includes the latest two major versions of Java LTS, Java 8 and Java 11 by default.

In this tutorial, we’re gonna explain how to install one or more Java OpenJDK versions on CentOS 8.

Installing java OpenJDK 11 :

Use dnf command below as root (or with sudo if you are not logged in as root) :

[root@server ~]# dnf install java-11-openjdk-devel

Once the installation is complete, check the version :

[root@server ~]# java -version
openjdk version "11.0.12" 2021-07-20 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.12+7-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.12+7-LTS, mixed mode, sharing)

As you can see, the version I installed is 11.0.12.

You can install a minimal version of OpenJDK 11 by typing the command below :

[root@server ~]# dnf install java-11-openjdk-headless

Installing OpenJDK 8 :

Version 8 is still used and supported, if your applications require version 8 you can install it with the command below :

[root@server ~]# dnf install java-1.8.0-openjdk-devel

Check the version :

[root@server ~]# java -version
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)

You can install a minimal version of OpenJDK 8 by typing the command below :

[root@server ~]# dnf install java-1.8.0-openjdk-headless

Set the default version of java :

If you have installed more than one version and you want a specific version to be started by default, use the following command :

[root@server ~]# alternatives --config java
There are 2 programs which provide 'java'.
Selection Command
   1 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.12.0.7-0.el8_4.x86_64/bin/java)
*+ 2 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.302.b08-0.el8_4.x86_64/jre/bin/java)
Enter to keep the current selection[+], or type selection number:

This command shows you the versions installed on your system and the one started by default (java-1.8.0-openjdk.x86_64 in my case)

If you wish to specify another version, enter the corresponding number.

In my case I chose version 11 :

[root@server ~]# alternatives --config java
There are 2 programs which provide 'java'.
Selection Command
+ 1 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.12.0.7-0.el8_4.x86_64/bin/java)
* 2 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.302.b08-0.el8_4.x86_64/jre/bin/java)

JAVA_HOME environment variable :

The JAVA_HOME is used by some Java applications to determine where Java is installed and to specify the Java version used to run the application.

To add the environment variable, create a file in the directory /etc/profile.d :

[root@server]# vim /etc/profile.d/java.sh
[root@server profile.d]# cat java.sh
JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk"

Use the commande below to save modifications :

[root@server]# source /etc/profile.d/java.sh

Check that the JAVA_HOME environment variable has been correctly set :

[root@server]# echo $JAVA_HOME
/usr/lib/jvm/java-1.8.0-openjdk