How to install Java on CentOS 7

Updated 

4 min read

Java is one of the most popular programming languages in the world, used to build different kinds of applications and systems.

This tutorial describes how to install various versions and implementations of Java on CentOS 7. We’ll show you how to install OpenJDK as well as Oracle Java.

Prerequisites

Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges.

Java variations

Java is distributed in three different editions, Standard Edition (SE), Enterprise Edition (EE), and Micro Edition (ME). This tutorial covers the Java SE (Standard Edition) edition.

There are two different Java packages in CentOS 7, Java Runtime Environment (JRE) and the Java Development Kit (JDK). If you only want to run Java programs then you need JRE which contains just the Java Runtime Environment and if you are Java developer then you will need JDK which includes JRE and development/debugging tools and libraries.

There are also 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.

If you are not sure which Java implementation and version to install, the general recommendation is install to install OpenJDK 11 JDK which is the current LTS version of Java.

Prerequisites

Before starting with the tutorial, make sure you are logged in as a user with sudo privileges.

Install OpenJDK

OpenJDK, the open source implementation of the Java Platform is the default Java development and runtime in CentOS 7. The installation is simple and straightforward.

Install OpenJDK 11 JDK

At the time of writing, OpenJDK 11 is the current LTS version of Java and the recommended version to install. You can install it using yum by typing the following command:

sudo yum install java-11-openjdk-devel

Verify the installation, by running the following command which will print the Java version:

java -version

The output will look something like this:

openjdk version "11.0.3" 2019-04-16 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.3+7-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.3+7-LTS, mixed mode, sharing)

That’s it! At this point, you should have successfully installed Java on your CentOS system.

Install OpenJDK 11 JRE

You can install OpenJDK 11 JRE using yum by typing the following command:

sudo yum install java-11-openjdk
JRE is a subset JDK and if you already installed the JDK package you do not need to install this one.

Install OpenJDK 8 JDK

Java 8 is still the most widely-used version of Java. If your application requires the older Java 8 to be installed on your CentOS 7 system, install it by running the following command:

sudo yum install java-1.8.0-openjdk-devel

Install OpenJDK 8 JRE

Same as above if your application requires OpenJDK 7 JRE you can install it with yum by running the following command:

sudo yum install java-1.8.0-openjdk

Install Oracle Java

In this section, we will go through the steps of installing Oracle Java. Oracle packages are available to download only from their official website.

Before installing Oracle Java make sure you read the Oracle JDK License. The license permits only non-commercial use of the software, such as personal use and development use.

You can download the Oracle Java .rpm packages from the Java SE Downloads page. To download Oracle Java you need to register on the Oracle site.

Once you download the package, use the following command to install it:

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

Set the default version

You can check the default Java, with:

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

If you have multiple Java versions installed on the server you can change the default version using the alternatives system utility:

sudo alternatives --config java

The output should look similar to the following:

There are 2 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
   1           java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.3.7-0.el7_6.x86_64/bin/java)
*+ 2           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.212.b04-0.el7_6.x86_64/jre/bin/java)

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

To change the default Java version, just enter the number when prompted and hit Enter.

Uninstall Java

If you, for any reason want to uninstall the Java package, you can uninstall it as any other package installed with yum.

For example, if you want to uninstall the java-1.8.0-openjdk-devel package simply run:

sudo yum remove java-1.8.0-openjdk-devel

Conclusion

Now that you have learned how to install and manage different Java versions on your CentOS server, your next step could be to install one of the many applications that run on Java, such as Tomcat, JBoss/WildFly, Apache Maven, Glassfish, Elasticsearch, Cassandra, Jenkins, Gradle ..etc

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