Java is one of the most widely used programming languages in enterprise software, and RHEL 8 makes it straightforward to install and manage multiple Java versions through its built-in package manager. Whether you are building web applications, microservices, or command-line tools, having a proper Java development environment is the first step. In this tutorial you will install OpenJDK, configure your environment variables, compile and run a simple program, and add Maven to enable dependency management. All commands are run as a user with sudo privileges on RHEL 8.

Prerequisites

  • A RHEL 8 server or workstation with a registered subscription (or CentOS Stream 8 / AlmaLinux 8 / Rocky Linux 8)
  • A user account with sudo privileges
  • Access to the default AppStream and BaseOS repositories
  • Basic familiarity with the Linux command line

Step 1 — Install OpenJDK with dnf

RHEL 8 ships OpenJDK 11 and OpenJDK 17 in its AppStream repository. Install the JDK package (which includes both the runtime and the compiler) using dnf.

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

If you need Java 17 instead, replace the package names:

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

Both versions can coexist on the same machine. The -devel package provides the compiler (javac), header files, and other development tools needed to build Java programs.

Step 2 — Select the Active Java Version with alternatives

RHEL uses the alternatives system to manage which Java binary is the default when multiple versions are installed. List available runtimes and choose one interactively:

sudo alternatives --config java

You will see a numbered list of installed JREs. Enter the number corresponding to the version you want and press Enter. Run the same command for javac to align the compiler:

sudo alternatives --config javac

Step 3 — Set the JAVA_HOME Environment Variable

Many build tools and applications expect the JAVA_HOME variable to point to the JDK installation directory. Create a profile script so it is set for all users at login:

sudo tee /etc/profile.d/java.sh << 'EOF'
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk
export PATH=$PATH:$JAVA_HOME/bin
EOF

Apply the changes to your current shell session:

source /etc/profile.d/java.sh

If you installed Java 17, replace java-11-openjdk with java-17-openjdk in the path above. You can confirm the correct path by running dirname $(readlink -f $(which java)) and moving one level up to the jvm directory.

Step 4 — Verify the Installation

Check that both the runtime and compiler are available and report the expected version:

java -version
javac -version
echo $JAVA_HOME

You should see output similar to openjdk version "11.0.x" and javac 11.0.x. If the version does not match your expectation, re-run alternatives --config java to switch it.

Step 5 — Compile and Run a Hello World Program

Create a minimal Java source file to confirm the full development toolchain is working:

mkdir -p ~/javatest && cd ~/javatest

cat > HelloWorld.java << 'EOF'
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello from RHEL 8!");
    }
}
EOF

javac HelloWorld.java
java HelloWorld

If everything is configured correctly the terminal will print Hello from RHEL 8!. The compiled bytecode file HelloWorld.class is also created in the same directory and can be distributed to any machine running a compatible JRE.

Step 6 — Install Apache Maven

Maven is the most common build and dependency management tool for Java projects. It is available directly from the AppStream repository:

sudo dnf install -y maven
mvn --version

The output should show the Maven version along with the Java version it is using. Maven stores downloaded dependencies in ~/.m2/repository. If your environment requires a corporate or internal mirror, you can point Maven at it by editing (or creating) ~/.m2/settings.xml and adding a <mirrors> block pointing to your Nexus or Artifactory instance.

Conclusion

You have successfully installed OpenJDK on RHEL 8, configured the JAVA_HOME environment variable system-wide, verified the toolchain by compiling and running a Java program, and installed Maven for project dependency management. Your RHEL 8 system is now ready for Java development. You can install additional JDK versions at any time and switch between them non-destructively with alternatives --config java.

Next steps: How to Install Maven and Gradle on RHEL 8, How to Deploy a Spring Boot Application on RHEL 8, and How to Configure a Java Application as a systemd Service on RHEL 8.