How to Install OpenJDK 11 On CentOS 8

How to Install OpenJDK 11 On CentOS 8

Learn what's involved in installing OpenJDK on CentOS 8, and how you can test your OpenJDK installation through a simple "Hello World" application.

Java is one of the world's most popular programming languages. Software written in Java can be compiled and run on any system, making Java a versatile platform that can be used to create anything from software to basic web applications. This guide will show you how to install the Open Java Development Kit (OpenJDK) 11 on CentOS 8.

OpenJDK is the free and open-source implementation of the Oracle Java Standard Edition (Java SE) Development Kit. OpenJDK and Java SE are equivalent JDKs that include a Java runtime environment (JRE) and tools for developing and compiling Java applications.

While there are many available versions of OpenJDK, version 11 is the latest Long-Term-Support (LTS) release as of the time of this guide's publication. For this reason, OpenJDK 11 is the recommended version for developing production applications.

Install OpenJDK

Install the OpenJDK 11 development kit, which includes OpenJRE 11:

sudo yum install java-11-openjdk-devel

Alternatively, if you simply want to run Java applications that you have already downloaded, you can choose to only install OpenJRE 11:

sudo yum install java-11-openjdk

Note: While you can run Java applications directly with the JRE, your applications will be compiled every time they are executed. This is generally slower than running applications that have already been compiled into Java bytecode, and may not be suitable if you plan to execute applications many times.

Check the version of the JRE to verify that it has been properly installed:

java -version

As of the time of this publication, this command should return:

openjdk version "11.0.7" 2020-04-14 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.7+10-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.7+10-LTS, mixed mode, sharing)

If you have chosen to install the full OpenJDK development kit, check the version of the compiler as well:

javac -version

As of the time of this publication, this command should return:

javac 11.0.7

Set Environment Variables

This section will instruct you on how to set the JAVA_HOME and PATH environment variables to help ensure that your Java applications will run without issue.

Open the ~/.bashrc startup file using the text editor of your choice and add the following definitions at the end of the file:

[...]
export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java))))
export PATH=$PATH:$JAVA_HOME/bin

NOTE: If you are using a shell other than Bash, such as Zsh, you may need to add these lines in a different startup file instead. In the case of Zsh, this would be the ~/.zshrc file.

Save the changes and exit your text editor.

Reload the ~/.bashrc file:

source ~/.bashrc

Verify that the JAVA_HOME and PATH variables were set correctly:

echo $JAVA_HOME
echo $PATH

The JAVA_HOME variable should be set to the directory that contains your OpenJDK installation, and the PATH variable should include the directory that contains the OpenJDK binary files.

Test the Java Installation (Optional)

To test your Java installation, write a sample HelloWorld Java application and run it with the JRE.

Open a text editor and add the following lines in a file labeled HelloWorld.java to create a simple function that prints "Hello Java World!":

public class HelloWorld {
public static void main(String[] args) {
    System.out.println("Hello Java World!");
}
}

Run the application using the JRE:

java HelloWorld.java

If the installation has been successful, the output will be:

Hello Java World!

If you have installed the full OpenJDK development kit, you can compile your application into a bytecode class file prior to running it for faster execution time.

Compile the application you have written:

javac HelloWorld.java

Confirm that the HelloWorld.class file was written to your current directory:

ls -l HelloWorld.class

Run the compiled HelloWorld function using the JRE:

java HelloWorld

The output should again be:

Hello Java World!
How to Code Hello World in Java
In this tutorial, we will learn how we to write the classic Hello World program using the Java programming language.

Ending...

In this post, you learned how to install the Open Java JDK and JRE 11 on CentOS 8. You also learned how to create a hello world. If you are just getting started in Java, learn the fundamentals of a hello world program in great detail in the resource below.

How to Install the OpenJDK and JRE 11 on Debian
Learn what’s involved in installing OpenJDK on Debian, and how you can test your OpenJDK installation through a simple “Hello World” application
How to Code Hello World in Java
In this tutorial, we will learn how we to write the classic Hello World program using the Java programming language.

If you enjoy our content, please consider buying us a coffee to support our work:

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.