How to Install Java on CentOS and Rocky Linux

November 7, 2024

Introduction

Java is a popular programming language for a wide range of applications, from enterprise software to games. Its robustness, versatility, extensive libraries, and strong community support make it a top choice for many development teams.

This step-by-step guide will show you how to install Java on CentOS and Rocky Linux.

How to Install Java on CentOS and Rocky Linux.

Prerequisites

  • A maintained/supported version of CentOS or Rocky Linux.
  • Command-line access.
  • Administrative privileges on the system.

Java Versions

Java is distributed through three major platforms:

  • Java Standard Edition (Java SE) provides the core libraries for basic functionalities like input/output, networking, and security.
  • Java Micro Edition (Java ME) is designed for application development on resource-constrained devices such as mobile phones and smart cards.
  • Java Enterprise Edition (Java EE or Jakarta EE) is used in large-scale, distributed enterprise applications.

The Java SE platform consists of two open-source Java packages:

  • Java Development Kit (Open JDK) for developing and programming with Java.
  • Java Runtime Environment (Open JRE) for running Java-based applications.

The commercial SE implementation is called Oracle Java. The Oracle JDK can be downloaded for free from the official Oracle website. However, the free license only allows non-commercial use of the software.

Install Java on CentOS and Rocky Linux

The following sections provide steps to install OpenJDK, OpenJRE, and Oracle Java on a CentOS or Rocky Linux system.

Install OpenJDK

Proceed with the steps below to install OpenJDK on your system:

1. Update the package repository list:

sudo yum update

2. Install the Java Development Kit with the following command:

sudo yum install java-devel

The output shows the version of OpenJDK that is to be installed on the system. Type Y and press Enter to start the installation.

output displaying the JDK to be downloaded and installed.

Install OpenJRE

Java Runtime Environment (OpenJRE) is a subset of OpenJDK. Therefore, the java-devel package includes both tools. Install OpenJRE by typing:

sudo yum install java-devel

Install Oracle Java

The official Oracle JDK is unavailable for download from a local repository. Install it by following the steps below:

1. Visit the Java SE Downloads page.

2. Select the desired Java SE version on the webpage and click the download link for the RPM package.

Download the RPM package from the Java SE Downloads page.

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

sudo yum localinstall [path]/jre-[version]-linux-x64.rpm

For example, to install the jdk-21_linux-x64_bin.rpm package located in the Downloads subdirectory, type the following:

sudo yum localinstall Downloads/jdk-21_linux-x64_bin.rpm

Enter Y and wait for the installation to finish.

Installing the Java RPM file.

Verify Java Is Installed on Your System

The following command shows which Java release is default on the system and confirms the installation was successful:

java –version

The system prints the version number and other relevant information.

The default version of Java installed on the system.

Install Specific Version of Java

Install other versions of Java by adding the version number to the installation command. To see the available versions, list them with yum search:

yum search openjdk-devel

The output shows a list of versions hosted in the repository.

Different available versions of OpenJDK.

To install a specific version, use the following command:

sudo yum install java-[version]-openjdk-devel

Replace [version] with the actual version number. For example:

sudo yum install java-17-openjdk-devel

How to Set Default Java Version

Multiple versions of Java can coexist on a system. The user can decide which one is the default one. To do so:

1. List the installed versions on your computer:

sudo alternatives --config java

The example image below shows three installed alternatives: OpenJDK 11, Oracle Java 21, and OpenJDK 21.

The command to show all installed java versions on your computer.

2. Type a number from the Selection column and press Enter.

How to Set JAVA_HOME Environment Variable

Setting the JAVA_HOME environment variable helps other applications access the Java installation path. Proceed with the steps below to set the environment variable:

1. Locate the Java installation:

sudo update-alternatives --config java

The output shows the path for each Java package installed on the system.

Finding the path for the JAVA_HOME environment variable.

2. Select and copy the path you want to add to the variable.

3. Open the .bash_profile file with a text editor such as Nano:

nano .bash_profile

4. At the bottom of the file, add a line that specifies the value of JAVA_HOME:

JAVA_HOME="/your/installation/path/"

For example, to copy the installation path for Java 11 listed in the example image above, type the following:

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-11.0.3.7-0.el7_6.x86_64/bin/java"
Editing the bash_profile file.

5. Save the file and exit.

6. Apply the changes:

source .bash_profile

How to Uninstall Java on CentOS and Rocky Linux

To remove any of the Java packages installed, use the yum remove command:

sudo yum remove java

Confirm the process by typing Y and pressing Enter.

Remove Java packages.

The command above removes all the installed Java instances from the system. To uninstall a specific Java version, add the version number in the command. For example, to remove OpenJDK 11, use the following command:

sudo yum remove java-11-openjdk-devel

Conclusion

This tutorial taught you how to install Java on CentOS and Rocky Linux. The article also showed how to change the default version and set the home environment variable.

If you are interested in Java programming, read our article on the best Java IDEs.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
How to Install Java on Ubuntu
November 7, 2024

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