How to Install Java on CentOS 8

Install Java on CentOS 8

In this article, we will have explained the necessary steps to install and configure Java on CentOS 8. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

Java is one of the most popular programming languages used to build different kinds of applications and systems. Applications developed in Java are scalable, flexible, and maintainable. Over 3 billion devices run on Java.

Install Java on CentOS

Step 1. The first command will update the package lists to ensure you get the latest version and dependencies.

sudo yum install epel-release
sudo yum update

Step 2. Install Java on CentOS.

To install the OpenJDK 11 on CentOS 8, run the following command:

sudo yum install java-11-openjdk-devel

Confirm the installation and check the version by typing the following command:

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

Step 3. Setting up default Java Version.

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

$ sudo 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.4.11-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.222.b10-0.el8_0.x86_64/jre/bin/java)

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

Setting the JAVA_HOME Environment Variable:

  • 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 system-wide setting, use a script inside the /etc/profile.d directory:
sudo nano /etc/profile.d/java.sh

Add the following line to the end of file:

JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk"

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/java.sh

Now verify JAVA_HOME path typing:

echo $JAVA_HOME

Congratulation, you have learned how to install and configure Java on CentOS 8. If you have any questions, please leave a comment below.