How to Install Java OpenJDK on CentOS

Sometimes, to run certain apps or tools on a Linux server, you need to have Java installed. One of the most common and free versions of Java is called OpenJDK. In this guide, I’ll show you how to install OpenJDK on a CentOS system, step by step.

I’ll keep things simple and clear. You don’t need to be a computer expert. If you follow along, you’ll get it running just fine.


What Is Java and OpenJDK?

Java is a programming language. It’s used to build many programs, websites, and mobile apps. You’ve probably used apps that rely on Java without even knowing it.

OpenJDK stands for Open Java Development Kit. It’s a free, open-source version of Java. Developers all over the world use it to write and run Java code.

You might see different versions of Java out there. For example:

  • Oracle Java
  • OpenJDK
  • Amazon Corretto

I personally prefer OpenJDK because it’s open-source, easy to install, and works well on CentOS.


What Is CentOS?

CentOS is a Linux operating system. It’s popular for servers because it’s stable and doesn’t change too often. I’ve used CentOS on several of my own web servers.

If you’re using CentOS 7 or 8, this guide will work for you. I’ll also share a few tips in case you’re using an older or newer version.


Why You Might Need Java

Java isn’t installed by default on CentOS. But sometimes you need it to:

  • Run Minecraft servers
  • Use tools like Jenkins or Apache Tomcat
  • Develop Java applications

If you’ve seen an error message like “java: command not found”, that means it’s time to install it.


Step 1: Check If Java Is Already Installed

Before installing anything, let’s see if Java is already there.

Run this command in your terminal:

java -version

If Java is installed, you’ll see something like:

openjdk version "1.8.0_292"

If you get an error like:

bash: java: command not found

Then Java isn’t installed yet. Let’s fix that.


Step 2: Update Your System

This is a good habit before installing any new software.

Run:

sudo yum update -y

This updates the package list and makes sure everything is ready.


Step 3: Choose a Java Version

OpenJDK comes in different versions. Some popular ones are:

  • Java 8 – Older, still used by many apps
  • Java 11 – More modern, still very stable
  • Java 17 – A long-term support (LTS) version

I usually go with Java 11 if there’s no specific requirement. But if you need a certain version, go with that one.


Step 4: Install OpenJDK

Let’s install OpenJDK using yum, the CentOS package manager.

Install Java 11:

sudo yum install java-11-openjdk-devel -y

Or install Java 8:

sudo yum install java-1.8.0-openjdk-devel -y

The -devel package includes tools for both running and developing Java apps.

After installation, check the version:

java -version

You should now see the version you installed.


Step 5: Set the Default Java Version (Optional)

Sometimes, you may have more than one Java version installed. You can set the one you want to use by default.

Run this command:

sudo alternatives --config java

It will list all installed Java versions. You’ll see something like:

There are 2 programs which provide 'java'.
  Selection    Command
-----------------------------------------------
* 1           /usr/lib/jvm/java-11-openjdk/bin/java
  2           /usr/lib/jvm/java-1.8.0-openjdk/bin/java

Enter to keep the current selection[+], or type selection number:

Pick the number for the version you want. Hit enter. Now that version is the default.


Step 6: Set JAVA_HOME (Optional but Useful)

Some apps need to know where Java is installed. That’s what JAVA_HOME is for.

First, find the path:

readlink -f $(which java)

You’ll see something like:

/usr/lib/jvm/java-11-openjdk-11.0.20.0.8-1.el8_8.x86_64/bin/java

Now copy the folder path (without /bin/java), for example:

/usr/lib/jvm/java-11-openjdk-11.0.20.0.8-1.el8_8.x86_64

Then add this line to your .bash_profile or .bashrc file:

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.20.0.8-1.el8_8.x86_64
export PATH=$JAVA_HOME/bin:$PATH

After saving, run:

source ~/.bashrc

Now, run this to check:

echo $JAVA_HOME

It should show the path you set.


Three Helpful Lists

Versions of OpenJDK You Might Use

  • OpenJDK 8: Still widely supported
  • OpenJDK 11: Great for most new projects
  • OpenJDK 17: Modern, with long-term support

Commands You’ll Use Often

  • java -version – check installed Java
  • sudo yum install – install packages
  • sudo alternatives --config java – switch between versions

Benefits of Installing OpenJDK

  • It’s free and open-source
  • Works well on most Linux servers
  • Supported by many apps and platforms

A Quick Note About javac

If you plan to write Java code (not just run it), you also need the Java compiler, which is javac.

If you installed the -devel package, you already have it.

Check it like this:

javac -version

If that works, you’re good to go.


A Tip from My Own Setup

I’ve used OpenJDK on both small VPS servers and full-blown production environments. It runs smoothly, needs little memory, and doesn’t cause problems.

Once, I tried using Oracle Java, but the setup was more complicated. OpenJDK has always been easier for me. Unless a specific app requires Oracle Java, I stick with OpenJDK every time.


What If You’re Using CentOS Stream or AlmaLinux?

CentOS 8 reached end of life, so you might be using CentOS Stream or AlmaLinux instead. Good news: this guide mostly still works. But instead of yum, you may use dnf, like this:

sudo dnf install java-11-openjdk-devel -y

That’s the only big change.


Final Thoughts

Installing OpenJDK on CentOS isn’t hard. Once you do it once or twice, it becomes second nature.

Whether you’re running apps, building a website backend, or learning Java for school, having the right Java setup is key.

If you’re just getting started, I suggest going with OpenJDK 11. It’s modern, stable, and widely supported.

What Java version do you need? Have you tried OpenJDK before, or are you switching from another version? Let me know if you’d like a quick guide on how to uninstall older Java versions safely too.

Leave a Reply