Java is incredibly popular on servers, and if you plan on using RHEL 8 / CentOS 8, you’ll need to install it. There are a couple of ways to install Java on RHEL, both from the open source OpenJDK packages and directly from Oracle.
In this tutorial you will learn:
- How to Install OpenJDK 8
- How to Install OpenJDK 11
- How to Install Oracle Java 8 JRE
- How to Install Oracle Java 8 JDK
- How to Switch Java Versions
Software Requirements and Conventions Used
|Requirements, Conventions or Software Version Used
|RHEL 8 / CentOS 8
|Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
How to Install OpenJDK 8
OpenJDK 8 is available right in the default RHEL 8 / CentOS 8 repositories. If that’s the version of Java that you need, go ahead and install the package with DNF.
# dnf install java-1.8.0-openjdk-devel
How to Install OpenJDK 11
OpenJDK 11 is a bit newer version of Java, but the Red Hat included it in RHEL 8 / CentOS 8 to “future proof” the release and cater to newer application that may arise in RHEL’s 10 year lifespan. If you’re looking for the latest from Java, install it with DNF.
# dnf install java-11-openjdk-devel
How to Install Oracle Java 8 JRE
There are always reasons to install the official version of Java straight from Oracle. Since Oracle Linux is essentially a clone of RHEL, there’s no problem getting Java this way too.
Head over to Oracle’s Java download page, and download the latest 64bit RPM. It’s at the bottom of the link list.
When you have your RPM, open a terminal, and use DNF to install the file locally.
# dnf install /home/user/Downloads/jre-8u201-linux-x64.rpm
How to Install Oracle Java 8 JDK
If you’re looking to develop with Java, you’re going to need the JDK too. Oracle provides a package for that as well. Go to the Oracle JDK download page, and locate the Linux x64 RPM download under the first “Java SE Development Kit” heading. Be sure to accept the license agreement first.
Once you have your RPM, use DNF to install it again, just like before with the JRE.
# dnf install /home/user/Downloads/jdk-8u201-linux-x64.rpm
How to Switch Java Versions
You can have multiple versions of Java on the same system. In order to make full use of any one of them, you’ll need to be able to switch between releases easily, and you can using simple tools built in to RHEL. Begin by checking which version of Java you’re currently running.
$ java -version
Now, you can use
alternatives to list out the available versions of Java and enable the one you want to use.
# alternatives --config java
You’ll see a list of the available Java versions. Enter the number corresponding to the one you want to use, or press Enter to keep your current one.
You’re officially ready to start working with Java onRHEL 8 / CentOS 8. You don’t need to install all of these, and OpenJDK will probably be suitable for most applications. That said, it’s nice to have options, and RHEL 8 / CentOS 8 certainly provides enough of them.