How to Install Java on CentOS
OpenJDK is the open-source Java implementation packaged in the CentOS repositories, so installing it needs no third-party repo. This guide covers CentOS 7 (yum) and CentOS 8 / Stream (dnf) — the package names are identical, only the package manager differs.
Step 1: Update the package index
Refresh the repository metadata so you install the current build:
sudo yum update -y
On CentOS 8 or Stream, use dnf instead:
sudo dnf update -y
Step 2: Install the OpenJDK runtime
The runtime (JRE) is enough to run Java applications. Java 11 is the long-term support release available on CentOS:
sudo yum install java-11-openjdk -y
If your application needs Java 8, install java-1.8.0-openjdk instead. To see everything available:
yum search openjdk
Step 3: Install the development package
If you intend to compile Java rather than only run it, you also need the JDK, which provides javac:
sudo yum install java-11-openjdk-devel -y
Step 4: Confirm the packages are installed
Ask the RPM database what landed:
rpm -qa | grep openjdk
You should see both the runtime and, if you installed it, the -devel package listed.
Step 5: Check the Java version
java -version
The output confirms the active runtime:
openjdk version "11.0.21" 2023-10-17 LTS
OpenJDK Runtime Environment (build 11.0.21+9-LTS)
OpenJDK 64-Bit Server VM (build 11.0.21+9-LTS, mixed mode, sharing)
If you installed the devel package, check the compiler too:
javac -version
Setting JAVA_HOME
Many applications — Tomcat, Maven, Elasticsearch — expect JAVA_HOME to be set. Find the install path, then export it:
sudo update-alternatives --config java
Take the path shown, drop the trailing /bin/java, and add it to your profile so it survives a reboot:
echo 'export JAVA_HOME=/usr/lib/jvm/java-11-openjdk' | sudo tee -a /etc/profile.d/java.sh
source /etc/profile.d/java.sh
echo $JAVA_HOME
Running more than one Java version
You can install several JDKs side by side and switch the system default with the alternatives system:
sudo alternatives --config java
Pick the number for the version you want. Run the same command with --config javac to switch the compiler independently of the runtime.
That is the whole install: update, install the runtime, add the JDK if you compile, verify, and point JAVA_HOME at it.

