Java is a popular object-oriented programming language used for building applications.
To run and develop an application in Java, you need to install the Java Development Kit (JDK). There are two different implementations of JDK – OpenJDK and Oracle JDK.
- OpenJDK is the free, open-source version of Java, available under the GPL v2 license.
- Oracle JDK is a paid, full-featured, Java software-package.
In this guide, learn how to install Java on a CentOS 8 server, including the OpenJDK and the Oracle JDK implementation.
- A system with CentOS 8 installed
- A user account with sudo or root privileges
- Access to a terminal window / command line (Search > terminal)
- An Oracle account (only required for Java JDK)
Install OpenJDK on CentOS 8
Open JDK is the free version of Java that consists of the Java Runtime Environment (JRE) and Development Tools. To run and develop Java applications, you need to install the complete package with its development kit.
Currently, there are two LTS versions of OpenJDK – OpenJDK 8 and OpenJDK 11. Oracle supports and updates both versions regularly. Although new releases come out every six months, they are maintained only until the next release. Therefore, it is not advisable to install anything other than version 8 or version 11 unless you have a good reason to do so.
Note: You can have more than one version of Java installed on your system. That’s helpful if you’re working on different projects that require different Java versions. Such configuration requires you specify the Java environment each time you work on a project.
Install OpenJDK 11
Open a terminal window and enter the following:
sudo yum install java-11-openjdk-devel
Confirm any prompts, allow the process to finish, and verify the installation with:
Install OpenJDK 8
To install the older LTS version, OpenJDK 8, use the following command:
sudo yum install java-1.8.0-openjdk-devel
Verify Java version 8 is now installed with:
If you are not developing in Java, but still need to run Java applications, you can skip installing the JDK and set up the Java Runtime Environment (JRE).
JRE is a combination of a Java Virtual Machine (JVM) and the libraries required for executing the code in runtime. It is the minimum requirement for running any Java application.
Just like with the Development Kit, there are two LTS versions of the Runtime Environment – JRE 8 and JRE 11.
To install JRE 11, run:
sudo yum install java-11-openjdk
To install JRE 8, use the command:
sudo yum install java-1.8.0-openjdk
Note: Remember – You don’t need to install OpenJRE if you already have OpenJDK. The Runtime Environment comes with the Java Development Kit by default.
Installing OpenJDK Headless
Another alternative to OpenJDK is only installing the headless version.
Just like OpenJRE, Java headless is part of OpenJDK and should not be installed on top of it.
The headless option is a minimal runtime environment, without a graphical interface, more suitable for server applications. It uses minimal system resources and doesn’t include keyboard or mouse support.
Install OpenJDK 11 headless by entering the following:
sudo yum install java-11-openjdk-headless
For OpenJDK 8 headless run:
sudo yum install java-1.8.0-openjdk-headless
Install Oracle JDK on CentOS 8
When installing Oracle Java on CentOS 8, make sure to download directly from the official source, Oracle. This requires creating an account on the Oracle website.
1. Open a web browser and navigate to the Oracle download page. Here you will find all the Java download links for different versions of Java SE.
Note: Java SE is the Standard Edition of the software, mainly used for developing portable desktop apps. An alternative to Java SE is Java Enterprise Edition (Java EE), more suitable for enterprise-level development.
2. Scroll down to see different versions of Java SE and find the one you need. In this article, we are sticking to the LTS release – Java 11.
The Java SE 11.0.5 (LTS) section includes a download button to install Oracle JDK (Java Development Kit). Unlike in the previous versions, this edition doesn’t offer the possibility to download Oracle JRE (Java Runtime Environment) as a separate package.
3. Click the blue Download button on the right, just below the Oracle JDK heading.
4. The link takes you to the next page of the download section and informs you about the Oracle JDK License Update. Make sure to review it before you download the software.
5. Scroll down and find the gray box labeled Java SE Development Kit 11.0.5. In it, you will find all the available packages for the software.
6. To download any of the packages, you first need to check the box that confirms you Accept License Agreement.
7. For CentOS 8, the Linux x64 .rpm file is a good package choice since it is easier to install. If you’re running a 32-bit operating system, use Linux x86.
8. To gain access to the file, log in with your Oracle account.
9. Allow the download to finish. Once it’s complete, open the terminal and enter the following:
sudo yum install /home/user/Downloads/jdk-11.0.5-linux-x64.rpm
Note: Replace user with the actual username. Additionally, if your filename is different, use the filename of your download.
9. Once the installation finishes, you can check the Java version with the following:
Setting the Default Java Version
You might have 2 different versions of Java on your system. To set one as the default , use the command:
sudo alternatives ––config java
The system displays a list of different Java versions. If you like the default, press Enter.
If you want to change it, type the number of the version you want, then press Enter.
Setting the JAVA_HOME Environment Variable
With the default version set, you can configure JAVA_HOME on your CentOS 8.
By doing so, applications compiled in this programming language can easily access Java’s installation path.
How you define the JAVA_HOME environment depends on which version of Java you have. To define the location of Java 8 or Java 11, refer to phoenixNAP’s instructional guide on How to Set JAVA_HOME Environment Variable.
Note: If you are new to setting environment variables, learn more by reading How To Set Environment Variables In Linux or How To Set Environment Variables In Mac.
You should now have one (or more) versions of Java running on CentOS 8.
Remember, if you only need support for running Java applications, choose JRE (Java Runtime Environment). Install JDK (Java Developer Kit) if you need to create Java applications.
In general, your system will be more stable for longer if you choose a LTS (Long-Term Support) version.