How to Install Java 11 & 12 on CentOS 7

June 23, 2019

Introduction

As a highly popular programming language, Java is used for developing anything from lightweight mobile to desktop applications.

This step-by-step guide will show you how to install Java on CentOS 7.

install java on centos

Note: These installation instructions apply for CentOS7 as well as versions 6 and 6.5, RHEL, and recent Fedora releases. For a dedicated Fedora guide, please refer to How To Install Java On Fedora.
And for other systems, check our tutorial on how to install Java on Windows.

Prerequisites

  • A user account with sudo privileges
  • Access to the command-line/terminal window
  • The yum package manager, included by default

Java Versions

Currently, there are four Java platforms available:

  1. Java Standard Edition (Java SE)
  2. Java Micro Editions (Java ME)
  3. Java Enterprise Edition (Java EE)
  4. JavaFX

In this document, we look at different packages within the Java SE.

There are two (2), open-source Java packages, Java Development Kit (Open JDK) and Java Runtime Environment (Open JRE). You use JRE for running Java-based applications, while JDK is for developing and programming with Java.

Also available is Java Oracle, another SE implementation, which has additional commercial features. You can find the official Oracle JDK through a third-party repository or on the official Oracle webpage. However, bear in mind its license only allows non-commercial use of the software.

Install Java On CentOS

You can install one or several Java packages on your machine. You can also decide which version you want on your system by installing a specific version number. The current default, Long-Term-Support (LTS) version is Java 11.

Install OpenJDK 11

1. Update the package repository to ensure you download the latest software:

sudo yum update

2. Then, install the Java Development Kit with the following command:

sudo yum install java-11-openjdk-devel

The output displays the downloaded and installed JDK, as in the image below:

output displaying downloaded and installed JDK

Install OpenJRE 11

Java Runtime Environment 11 (Open JRE 11) is a subset of OpenJDK. Therefore, both packages are included in the command:

sudo yum install java-11-openjdk

Install Oracle Java 11

1. The official Oracle JDK is not available for download from the local repository. You have to download the .rpm package from the Java SE Downloads page.

2. The latest release is Java SE 12. However, Oracle Java 11 is LTS, meaning it is the latest stable version.

Locate Java SE 11.0.3 (LTS) on the webpage and click on DOWNLOAD.

download the .rpm package from the Java SE Downloads page

3. After the download is complete, you can install the package with the command:

sudo yum localinstall jre-VERSION-linux-x64.rpm

Verify Java is Installed on Your System

The following command shows which Java release you have and confirms installation was successful:

java –version

If Java was successfully downloaded and installed, the output details should match the image below:

confirms java installation was successful

Install Specific Version of Java

You can also install any of the other versions of Java by changing the version number in the command.

Install JDK 8

You may decide to use Open JDK 8, instead of the default OpenJDK 11.

To do so, type in the following command in the terminal:

sudo yum install java-1.8.0-openjdk-devel

Install JRE 8

As a subset of Open JDK 8, you can install Open JRE 8 by typing in the command:

sudo yum install java-1.8.0-openjdk

Install Oracle Java 12

As previously mentioned, you have to download Oracle Java from its official page.

To install the newest version, simply download the appropriate package from their webpage and continue with the installation.

How to Set Default Java Version

Since you can have multiple versions of Java installed on your system, you can decide which one is the default one. To do so;

Run a command that shows all the installed versions on your computer:

sudo alternatives --config java
command to show all installed java versions on your computer

The image above shows that there are two alternatives to this system. These choices are represented by numbers 1 (Java 11) and 2 (Java 8).

To change the default version, type the associated number (in this case, 1 or 2) and press Enter.

How to Set JAVA_HOME Environment Variable

Setting the JAVA_HOME environment variable helps other applications compiled in this programming language to access Java’s installation path easily.

1. First, locate where Java is installed:

sudo update-alternatives --config java
Setting the JAVA_HOME environment variable

In the output, you can find the path for each Java package installed on the system.

  • /usr/lib/jvm/java-11-openjdk-11.0.3.7-0.el7_6.x86_64/bin/java (where OpenJDK 11 is located)
  • /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.212.b04-0.el7_6.x86_64/jre/bin/java (where OpenJDK 8 is located)

2. Once you see all the paths, copy the one of your preferred Java version.

3. Then, open the .bash_profile with any text editor. In this example, we use vim:

vim .bash_profile

4. At the bottom of the file, add a line which specifies the location of JAVA_HOME in the following manner:

JAVA_HOME=”/your/installation/path/”

For example, if we copy the installation path for Java 11 in the previous step, the added line in the text editor will be:

/usr/lib/jvm/java-11-openjdk-11.0.3.7-0.el7_6.x86_64/bin/java

How to Uninstall Java on CentOS

In case you need to remove any of the Java packages installed, use the yum remove command.

For example, to remove Open JDK 8 use the following command:

sudo yum remove java-1.8.0-openjdk-devel

Confirm the process by pressing y (yes), and it will uninstall the package from CentOS, as in the image below:

 remove Java packages

Conclusion

In this tutorial, you have learned how to install Java on CentOS 7.

We also covered changing the default version and setting the home environment variable. You are now ready to get started with Java!

If you are using a newer version of CentOS, check out our article on how to install Java 8 & 11 on Centos 8.

Was this article helpful?
YesNo
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
Next you should read
How to Install Java on Ubuntu
June 19, 2019

Java is one of the most popular programming languages used for developing anything from lightweight mobile to...
Read more
How to Install Tomcat 9 on CentOS 7
June 14, 2019

Apache Tomcat is an open source Java implementation package developed by the Apache Software Foundation. In...
Read more
How To Install PHP 7, 7.2 & 7.3 On CentOS 7
May 24, 2019

PHP is a programming language that's often used to automate server tasks. It's part of the LAMP (Linux,...
Read more
How to Implement Validation for RESTful Services with Spring
November 13, 2018

Data validation is not a new topic in web application development and here we take a brief look at data...
Read more