Guides 11792 Published by

The guide walks readers through installing Java on CentOS 9 Stream with minimal fuss, starting by checking the current installation and explaining why the default OpenJDK 11 may not suffice for modern applications. It then shows how to pull the latest LTS release of OpenJDK via dnf, switch between multiple Java versions using alternatives, and optionally configure environment variables in the user’s shell profile so JAVA_HOME is always set. For teams that require Oracle’s official distribution, step‑by‑step instructions are provided to download, unpack, link, and activate the JDK under /opt while ensuring it appears in the alternatives system. A concise troubleshooting section covers common errors such as missing executables, version mismatches after updates, and SELinux restrictions, and the final recap table distills the essential commands into a quick reference for busy developers.



Install Java on CentOS 9 Stream – Quick, Clean, and No‑Pain

If you’re trying to run a Spring Boot app or just want the “java -version” command to work, this is the straight‑to‑the‑point guide for getting Java up and running on CentOS 9 Stream.

1 Check What You’ve Got
java -version

If you see a version number, great—you’re already set. If it says “command not found” or points to an old 7/8 release from the legacy repo, skip ahead and install.

CentOS 9 Stream ships with OpenJDK 11 in its base repos, but that’s sometimes too old for modern libraries, so you’ll want the latest LTS (Java 17) or the Oracle build if your app demands it.

2 Install the Latest OpenJDK via DNF
sudo dnf install java-latest-openjdk

The java-latest-openjdk package pulls in the newest 17‑based release, keeping you on the current LTS without pulling in extra modules that bloat your system.

After installation:

java -version   # Should show something like “openjdk version "17.0.x"”

If you need Java 11 specifically (some legacy apps still want it), just run:

sudo dnf install java-11-openjdk
3 Set the Default Version (When You Have More Than One)

CentOS uses the alternatives system to manage multiple JDKs. If you installed both 11 and 17, switch with:

sudo alternatives --config java

You’ll see a list of installed binaries; type the number that corresponds to your chosen version.

Why bother? Some applications hard‑code the path to /usr/bin/java, so pointing alternatives correctly keeps the system happy.

4 Verify Environment Variables (Optional)

Add these lines to ~/.bashrc if you want $JAVA_HOME set automatically:

export JAVA_HOME=$(dirname $(readlink -f $(which java)) | sed 's|/bin||')
export PATH=$JAVA_HOME/bin:$PATH

Then reload the shell:

source ~/.bashrc

Now echo $JAVA_HOME should give you /usr/lib/jvm/java-17-openjdk-....

5 Oracle JDK: When You Need the “Official” Build

Some enterprise apps insist on Oracle’s distribution. The steps are a bit more hands‑on:

1. Download the tar.gz from Oracle’s site (you’ll need an account).

2. Extract to /opt:

   sudo mkdir -p /opt/oracle-jdk
   sudo tar xzf jdk-17_linux-x64_bin.tar.gz -C /opt/oracle-jdk --strip-components=1

3. Create a symlink so alternatives can see it:

   sudo alternatives --install /usr/bin/java java /opt/oracle-jdk/bin/java 2000

4. Pick it as default (if you have others):

   sudo alternatives --config java

5. Verify:

   java -version   # Should show “javac 17” with Oracle branding.

Why this route? If your build scripts check for an oracle flag or rely on proprietary features, the official JDK is the only way to satisfy them.

6 Troubleshooting Common Pitfalls
  • “Could not find java executable” – You probably installed Java in /usr/lib/jvm, but your shell hasn’t refreshed $PATH. Reboot or source ~/.bashrc.
  • Version mismatch after updates – CentOS may install a newer 11 while you’re still pointing to 17 via alternatives. Run sudo alternatives --config java again.
  • SELinux blocking execution – If SELinux is enforcing, you might see “Permission denied” errors when launching Java from certain directories. Use semanage fcontext or simply stick with the default package installation path.
7 Quick Recap for the Busy Fixer
Step Command What It Does
1 sudo dnf install java-latest-openjdk Pulls in Java 17 from the repo.
2 sudo alternatives --config java Chooses which JDK to run by default.
3 Add $JAVA_HOME to .bashrc Makes environment variables available everywhere.
4 (Optional) Oracle steps above Gives you the official build if required.

That’s all there is to it. Pick what your apps need, install with dnf, tweak alternatives if you’re juggling versions, and you’ll have Java ready for whatever you throw at it.