# Install Java on CentOS 8

# Install OpenJDK

You can use the yum command to install OpenJDK.

yum -y install java-11-openjdk-devel

Check the java version using the following command.

java -version

# Setup Environmental Variables

The most important part has come now, Java applications often require JAVA environment variables to be set in the system.

Create a new file under /etc/profile.d directory

sudo nano /etc/profile.d/java.sh

Set variables based on the Java location and version.

export PATH=$PATH:/usr/lib/jvm/java-11-openjdk-11.0.2.7-2.el8.x86_64/bin/
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.2.7-2.el8.x86_64/
export J2SDKDIR=/usr/lib/jvm/java-11-openjdk-11.0.2.7-2.el8.x86_64/

Load the environments into the current session.

source /etc/profile.d/java.sh