How can I install Java 8 on CentOS 8 / RHEL 8 / Rocky Linux 8?. On Red Hat Enterprise Linux (RHEL) 8, two major versions of Java are available – Java 11 and Java 8. Our last article addressed the installation of Java 11 on CentOS / RHEL 8. In this article, I’ll cover the installation of Java 8, specifically, JDK (Java Development Kit) 8 on CentOS 8 / RHEL 8 / Rocky Linux 8.
You can check features of RHEL 8 and optionally how to install RHEL 8 if you’re a new user.
Install Java 8 on CentOS 8 / RHEL 8 / Rocky Linux 8 Linux
To install JDK 8 on your CentOS 8 / RHEL 8 / Rocky Linux 8 server or workstation machine, Use the following command.
sudo yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
Check version to validate installation of Java 8 on CentOS 8 / RHEL 8.
$ java -version
openjdk version "1.8.0_362"
OpenJDK Runtime Environment (build 1.8.0_362-b08)
OpenJDK 64-Bit Server VM (build25.362-b08, mixed mode)
Set Java environment variables.
cat <<EOF | sudo tee /etc/profile.d/java8.sh
export JAVA_HOME=/usr/lib/jvm/jre-openjdk
export PATH=\$PATH:\$JAVA_HOME/bin
export CLASSPATH=.:\$JAVA_HOME/jre/lib:\$JAVA_HOME/lib:\$JAVA_HOME/lib/tools.jar
EOF
Activate Java environment.
source /etc/profile.d/java8.sh
Test the variables:
$ echo $JAVA_HOME
/usr/lib/jvm/jre-openjdk
$ echo $PATH
/home/rocky/.local/bin:/home/rocky/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/lib/jvm/jre-openjdk/bin
$ echo $CLASSPATH
.:/usr/lib/jvm/jre-openjdk/jre/lib:/usr/lib/jvm/jre-openjdk/lib:/usr/lib/jvm/jre-openjdk/lib/tools.jar
Create a simple Java application to see Java 8 working on CentOS 8 / RHEL 8 / Rocky Linux 8.
cat > HelloWorld.java <<EOF
public class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello World");
}
}
EOF
Compile your Java source code.
$ javac HelloWorld.java
This will create HelloWorld.class file, which is the bytecode embodiment of the source code. To run the HelloWorld.class file, use:
$ java HelloWorld
Hello World
You now have Java installed and configured on CentOS 8 / RHEL 8 / Rocky Linux 8. Enjoy your development!!.
Recommended books:
For Java 11, use how to install Java 11 on RHEL / CentOS 8.