install Java featured image

Installing Java on CentOS and Fedora

In the world of programming, Java has been one of the top programming languages. Java is a high-level programming language that follows the concept of object-oriented programming systems. It’s also a platform-independent language that generates cross-platform applications. Java is everywhere – banking and transactions, web frameworks, games (Minecraft), and even mobile operating systems (Android).

In this guide, we will walk you through the steps of installing Java on CentOS and Fedora.

Prerequisites

Performing system-level changes requires sudo privilege. However, it’s recommended to use a non-root user with sudo privilege while performing these actions. You can refer to our tutorial to learn how to manage sudo permission for users using the sudoers file. For in-depth CentOS server setup and configuration instructions, check out this tutorial.

Java Variations

There are a couple of different Java editions available:

  • Standard Edition- Java SE
  • Enterprise Edition-Java EE
  • Micro Edition-Java ME

While all of them have their share in the industry, most of the Java applications are developed using Java SE. This guide will demonstrate installing Java SE.

Java SE comes in two types of packages:

  • JRE: It stands for Java Runtime Environment. It’s important to execute any Java application. The JRE package consists of necessary binaries and libraries that provide a JVM (Java Virtual Machine) and utilities.
  • JDK: It stands for Java Development Kit. JDK includes all the necessary tools and libraries to develop and debug Java applications. Any JDK package will also include a copy of the JRE.

There are two major implementations of Java: Oracle Java and OpenJDK. OpenJDK is an open-source project that forms the primary basis of Java itself. Oracle Java implements OpenJDK with some proprietary code.

Both Java implementations have different licensing policies. Since Java 9, OpenJDK releases are licensed under GPLv2+CPE (GPLv2 with Classpath Exception). Oracle Java, on the other hand, is released under the OTN License Agreement for Java SE.

The licensing policy change created a big uproar in the Java community. Since then, there have been multiple flavors of OpenJDK available: AdoptOpenJDK, GraalVM, Liberica, etc.

Which Java SE to Choose?

With all the options available, it can be overwhelming to decide what the right Java variation for you is. This section will discuss the scope for all these options.

  • JDK vs. JRE

It’s a fairly straightforward choice. JDK, for the most part, is for Java developers. Are you willing to learn or develop Java applications? If yes, then JDK is the way to go.

If you just need to run Java programs only, then pick JRE. Installing JDK will also work but in terms of efficiency, it’s redundant.

  • OpenJDK vs. Oracle

Both OpenJDK and Oracle Java are available for free. However, depending on the deployment environment, Oracle Java may be subject to charge. The pricing has ties to the Java release cycle. Every six months, a new version of Java is released. With each new release, the older versions won’t receive any patch, bug fix, or improvements in the public codebase.

As for the deployment environment, Oracle Java is free as long as it’s being used for educational purposes and personal use. When it comes to the corporate production environment, it may be subject to charge. If companies need support for an older version of Java, then Oracle offers prolonged support for a price.

Are you an individual using Java for personal use only? Then either will work for you. Are you interested in deploying Java for your company/enterprise? Oracle Java is available with payment. Alternatively, you may have a look at OpenJDK flavors. The vanilla OpenJDK comes with no support but various OpenJDK flavors (AdoptOpenJDK, Amazon Corretto, Azul Zulu, etc.) offer long-term support for all the major releases for free.

  • OpenJDK flavor

In terms of functionality, all the OpenJDK flavors offer almost the same experience. It’s generally various minor (and some major) features that differentiate each of the OpenJDK flavors. The vanilla OpenJDK, in terms of patches and bug fixes, follows a similar pattern to Oracle Java. Once a new Java version is released, the older versions won’t receive any future updates. However, the older OpenJDK releases are still free to use.

This is where the other flavors come in. These OpenJDK flavors generally offer support for older and new releases for a longer time than Oracle officially offers. Some of these projects have sponsorship by industry giants.

Installing Java

At the time of writing this article, the latest LTS release is Java 11. It’s recommended to use Java LTS (long-term support) release as it will receive updates for the longest period. Java 11 LTS OpenJDK flavors are slated to receive support up to 2030.

  • Installing Oracle JDK
Oracle JDK 16

Oracle offers installable packages for Linux. JDK from Oracle is available as DEB, RPM, and TAR.GZ packages. Because we’re working with CentOS and Fedora, our target is the Oracle JDK RPM package.

Oracle only offers the latest JDK release available for public download. For older releases, you need an Oracle account to access them. At the time of writing, the latest Java release is Java 16. First, check out Oracle JDK 16 download page:

Java Download Page

To install the Java package on CentOS, run the following command:

Centos Localinstall

To install the Java package on Fedora, run the following command:

Fedora Localinstall
Oracle JDK 11

Check out the Oracle JDK 11 download page. Then, download the RPM package. It is now time to install the package. To install the RPM package on CentOS, run the following command:

To install the RPM package on Fedora, run the following command:

Oracle JDK 8

Although newer releases are made, Java 8 remains one of the most popular versions among developers. Understandably, the industry is slowly shifting towards newer releases but Java 8 still ranks high on popularity. It’s also available as installable RPM packages from the official Oracle website. Check out the Oracle JDK 8 download page. Next, download the RPM package on your system.

To install the RPM package on Fedora, run the following command:

To install it on CentOS, run the following command:

  • Installing OpenJDK
OpenJDK 16

OpenJDK packages are directly available from the official package servers. All you need is to tell the package manager to install the package. On both CentOS and Fedora, OpenJDK 16 is available as the package java-16-openjdk-devel.

Install OpenJDK 16 on CentOS:

Install OpenJDK 16 on Fedora:

OpenJDK 11

On both CentOS and Fedora, OpenJDK 11 is available as java-11-openjdk-devel.

Install OpenJDK 11 on CentOS:

Centos Openjdk 11 Install

Install OpenJDK 11 on Fedora:

Fedora Openjdk 11 Install
OpenJDK 8

Thankfully, OpenJDK 8 is also directly available from the official package servers. In both CentOS and Fedora, the OpenJDK package is labeled as java-1.8.0-openjdk-devel.

Install OpenJDK 8 on Fedora:

Install OpenJDK 8 on CentOS:

OpenJDK Flavors

OpenJDK flavors are an interesting option for both personal and corporate/enterprise users. Many of them offer long-term support for various Java releases without any cost.

  • Installing AdoptOpenJDK

AdoptOpenJDK is another OpenJDK flavor that’s quite popular. All the OpenJDK builds are available for free. It’s a project powered by Java developers, and vendors (Amazon, IBM, Azul, Microsoft, Red Hat, and others). AdoptOpenJDK binaries offer both community and commercial support.

To install AdoptOpenJDK, we will take advantage of the third-party repository that offers pre-built DEB and RPM packages for Debian/Ubuntu, CentOS/RHEL/Fedora, and openSUSE/SLES. The repository and available packages are briefly described by this article: AdoptOpenJDK — RPM and DEB files.

First, we need to install the third-party repo. It’s a bit more complex than other methods described in this guide. In short, we’ll have to manually create the repo entry. Create a new repo adoptopenjdk.repo. Note that you must run the command as the root user:

After that, enter the following code. Depending on your CentOS version, update the baseurl entry accordingly:

Centos adoptopenjdk

On Fedora, enter the following code instead. Depending on the Fedora version, update the baseurl entry accordingly. Don’t forget to run the command as root:

Fedora adoptopenjdk

The repo is now configured. Next, check if YUM/DNF picks up the new repo:

Centos repolist

Fedora repolist

Now, we can install the AdoptOpenJDK packages. Check out all the available packages:

To install AdoptOpenJDK 11 (hotspot) on Fedora, run the following command:

dnf Install adoptopenjdk hotspot

To install AdoptOpenJDK 11 (hotspot) on CentOS, run the following command:

yum Install adoptopenjdk hotspot

Testing Installation

After you’ve installed your JDK of choice, perform a short test to verify if the installation is working properly:

Java Version

Javac Version

Setting Default Java

If your system has multiple JDKs installed, then it may lead to conflicts. To avoid troublesome behavior, you should set one as the default JDK. We can set it using the alternatives command. Some programs require additional configuration of environment variables to determine the JDK location.

  • Using alternatives

On CentOS/Fedora, the alternatives command manages default commands through symbolic links. In the case of Java, it’s incredibly useful. To change the default Java, run the following alternatives command:

alternatives cmd

In the output, alternatives will print all the Java installations. Enter the Selection value of your preferred Java installation to set it as the default one.

  • Environment variables

There are a handful of environment variables dedicated to Java. These variables declare the location of the Java executables. The most popular environment variable is JAVA_HOME. Its value is the full path to the Java installation. Set the environment variable:

export JAVA_HOME

To set JAVA_HOME for all the users in the system, you have to declare its value under /etc/environment:

export env for all users

Final Thoughts

This guide demonstrated how to install Java on CentOS and Fedora. We have also presented the various Java versions so that you can choose the JDK that suits your style the best.

Java is a powerful programming language. It’s one of the few programming environments that place cross-platform development first. Almost any Java program will run on any other supported platform. As a developer, you’re free from platform anxiety. As a user, you can rest assured that the desired application will run on your system.

If you’d like to install Java on Ubuntu instead, check out this tutorial.

Happy computing!