Oct 7, 2023 4 min read

How to Install Java on CentOS 7

Install Java on CentOS 7 with our step-by-step tutorial. Java is a popular programming language known for its versatility and security.

Install Java on CentOS 7
Table of Contents

Choose a different version or distribution

Introduction

Before we begin talking about how to install Java on CentOS 7. Let's briefly understand - What is Java?

Java is a popular programming language known for its versatility and security. With Java, developers can create applications that run on different platforms.

It offers a wide range of libraries and is widely used for building desktop, web, and mobile apps. By learning Java, individuals can unlock numerous opportunities in software development.

In this tutorial, you will install Java on CentOS 7. We will also address some FAQs related to Java installation.

Advantages of Java

  1. Portability: Java code can run on different platforms, making it highly portable.
  2. High Security: Java includes built-in security features that protect against potential vulnerabilities.
  3. Vast Libraries: Java offers a broad range of libraries that simplify development and enhance functionality.
  4. Scalability: Applications built with Java can easily adapt and scale to handle increased workloads.
  5. Versatility: Java is widely used for desktop, web, and mobile app development, providing diverse opportunities for developers.

Prerequisites

  • Remember to log in as a user with the sudo privileges.

The Java variations

Java has three different editions which are distributed: The Standard Edition (SE), The Enterprise Edition (EE), and The Micro Edition (ME). Here we will proceed with installing the Java SE (Standard Edition).

The two main implementations of Java are OpenJDK and Oracle Java. These are almost similar to one another except for the fact that Oracle Java has a few additional commercial features.

Ubuntu repositories have two different Java packages: Java Runtime Environment (JRE), and the Java Development Kit (JDK).

JRE consists only of the Java Runtime Environment, which is apt for running Java programs. JDK on the other hand includes development/debugging tools and libraries which are suitable for Java developers.

In case you don't know which Java version to use, you should stick to the default OpenJDK version available for CentOS 7.

Installing OpenJDK

OpenJDK is the open-source implementation of Java-Platform. Further, it is the default Java development and runtime in CentOS 7.

Step 1 - Installing the OpenJDK-11 JDK

1) OpenJDK 11 is the current LTS version of Java and recommendation as well. You can install it with yum using the below command:

sudo yum install java-11-openjdk-devel

2) After that, verify the installation. You will do it by running the below command. It will print the Java version:

java -version

The output will look like this:

openjdk version "11.0.3" 2021-10-20 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)

You now have a successful installation of Java on your CentOS system.

Installing the OpenJDK 17 JRE

Now, install OpenJDK 17 JRE with yum using the below command:

wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.rpm
sudo rpm -ivh jdk-17_linux-x64_bin.rpm
💡
The JRE is a subset of JDK. If you already have an installation of JDK package then, you do not need to install this one.

Step 2 - Installing OpenJDK 8 JDK

1) Java 8 is the most used version of Java. If your application needs the older Java 8 installation on your CentOS 7 system, then use the below command:

sudo yum install java-1.8.0-openjdk-devel

Step 3 - Installing OpenJDK 8 JRE

1) The Oracle packages are available to download from their official website. Before installing Oracle Java, make sure to read the Oracle JDK License.

2) You will download Oracle Java .rpm packages from the Java SE Downloads page. To download Oracle Java, you will register on the Oracle site.

3) After, downloading the package. Use the below command to install:

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

Step 4 - Setting Up the Default Version

1) You can check the default Java version using the below command:

java -version

You will see the below output:

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)

2) If you have multiple Java version installations on the server, you can change the default version using the alternatives system utility:

sudo alternatives --config java

The output will look similar:

Output

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:

3) You can simply enter the number and hit Enter to change the default Java version.

Step 5 - Uninstalling the Java

1) So, if you want to uninstall the Java package, you can do it with yum. For instance, to uninstall the java-1.8.0-openjdk-devel package, just run:

sudo yum remove java-1.8.0-openjdk-devel

FAQs to Install Java on CentOS 7

Can I install a different version of Java on CentOS 7? 

Yes, you can install different versions of OpenJDK. For example, sudo yum install java-1.11.0-openjdk installs OpenJDK 11.

How can I set the Java environment variables on CentOS 7? 

Edit the /etc/environment file and add JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-<version> (replace <version> with the installed Java version).

How can I switch between different Java versions on CentOS 7? 

Use sudo alternatives --config java command to switch between multiple Java versions installed on your system.

Can I install Oracle JDK instead of OpenJDK on CentOS 7? 

Yes, you can install Oracle JDK on CentOS 7, but it requires manual installation. Refer to Oracle's documentation for the steps.

How do I update Java on CentOS 7? 

To update Java, run sudo yum update java-1.8.0-openjdk, replacing the version with the one you have installed.

How can I uninstall Java from CentOS 7?

To remove Java from CentOS 7, type sudo yum remove java-1.8.0-openjdk in the terminal.

Can I install Java using package managers like YUM or DNF? 

Yes, Java can be installed using package managers like yum or dnf. These package managers simplify the installation process and handle dependencies automatically.

Conclusion

We hope this detailed guide helped you to install Java on CentOS 7.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Blog - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.