dotlinux blog

How to Install Java 16 in Rocky Linux and AlmaLinux

Java is a versatile, cross-platform programming language and runtime environment used for building countless applications—from enterprise software to mobile apps. While Long-Term Support (LTS) versions like Java 11 or 17 are recommended for production, Java 16 (a feature release) introduces exciting improvements like Pattern Matching for instanceof, Records, and Vector API.

If you’re running Rocky Linux or AlmaLinux (both RHEL 8/9 derivatives) and need Java 16 for development or testing, this guide will walk you through two reliable installation methods:

  1. DNF Package Manager (using the Adoptium repo for easy updates)
  2. Manual Installation (for full control over the Java directory)

We’ll also cover setting environment variables, switching between Java versions, and troubleshooting common issues.

2026-04

Table of Contents#

Prerequisites#

Before you begin, ensure you have:

  • A running instance of Rocky Linux 8/9 or AlmaLinux 8/9 (64-bit).
  • Sudo privileges (to install packages and modify system files).
  • An active internet connection (to download packages or binaries).

1. Update System Packages#

Always start by updating your system to ensure you have the latest security patches and package dependencies:

sudo dnf update -y

This step prevents conflicts and ensures compatibility with new software installations.

The Adoptium Project (formerly AdoptOpenJDK) provides pre-built, open-source Java binaries that are compatible with RHEL-based distros like Rocky Linux and AlmaLinux. Using DNF (the default package manager) is the easiest way to install Java 16, as it handles dependencies and updates automatically.

Step 2.1: Add the Adoptium Repository#

  1. Import the Adoptium GPG Key (to verify package integrity):

    sudo rpm --import https://packages.adoptium.net/artifactory/api/gpg/key/public
  2. Create a Repository File for Adoptium: Open a text editor (e.g., nano) to create /etc/yum.repos.d/adoptium.repo:

    sudo nano /etc/yum.repos.d/adoptium.repo
  3. Paste the Repository Configuration (for RHEL 8/9-based distros):

    [Adoptium]
    name=Adoptium
    baseurl=https://packages.adoptium.net/artifactory/rpm/centos/8/$basearch
    enabled=1
    gpgcheck=1
    gpgkey=https://packages.adoptium.net/artifactory/api/gpg/key/public
    • For Rocky Linux 9/AlmaLinux 9, replace centos/8 with centos/9 in the baseurl.
  4. Save and Exit: Press Ctrl+OEnterCtrl+X.

  5. Refresh the DNF Cache to recognize the new repo:

    sudo dnf makecache

Step 2.2: Install Temurin 16 JDK#

Adoptium’s Java binaries are branded as Temurin. Install the Java 16 JDK (Java Development Kit—includes the compiler javac):

sudo dnf install temurin-16-jdk -y

If you only need the JRE (Java Runtime Environment—for running Java apps, not compiling), replace temurin-16-jdk with temurin-16-jre.

3. Manual Installation of Java 16#

If you prefer full control over the Java installation directory (e.g., for multiple Java versions or custom paths), use manual installation.

Step 3.1: Download Java 16 Binary#

  1. Go to the Adoptium Temurin 16 Releases page:
    https://adoptium.net/temurin/releases/?version=16

  2. Select:

    • Operating System: Linux
    • Architecture: x64 (most modern systems)
    • Package Type: Tar.gz
    • JVM Variant: HotSpot (default, optimized for performance)
  3. Copy the download link for the latest version (e.g., OpenJDK16U-jdk_x64_linux_hotspot_16.0.2_7.tar.gz).

  4. Download the Binary using wget (replace the URL with your copied link):

    wget https://github.com/adoptium/temurin16-binaries/releases/download/jdk-16.0.2%2B7/OpenJDK16U-jdk_x64_linux_hotspot_16.0.2_7.tar.gz

Step 3.2: Extract and Move to /opt#

  1. Extract the Tarball:

    tar -xzf OpenJDK16U-jdk_x64_linux_hotspot_16.0.2_7.tar.gz

    This creates a directory like jdk-16.0.2+7.

  2. Move the Directory to /opt: /opt is the standard location for third-party software on Linux. Use sudo to move the directory:

    sudo mv jdk-16.0.2+7 /opt/

Step 3.3: Configure Alternatives#

The update-alternatives tool manages multiple Java versions on your system. Register Java 16 with it:

  1. Register the java Command:

    sudo update-alternatives --install /usr/bin/java java /opt/jdk-16.0.2+7/bin/java 1602
    • /usr/bin/java: The system-wide java executable.
    • /opt/jdk-16.0.2+7/bin/java: Path to the Java 16 binary.
    • 1602: A priority number (higher = more preferred; use a unique value for each Java version).
  2. Register the javac Command (for compiling Java code):

    sudo update-alternatives --install /usr/bin/javac javac /opt/jdk-16.0.2+7/bin/javac 1602

4. Set Default Java Version#

If you have multiple Java versions installed, use update-alternatives to set Java 16 as the default:

sudo update-alternatives --config java

You’ll see a list of installed Java versions:

There are 2 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
*+ 1           /usr/lib/jvm/java-11-openjdk-11.0.20.0.8-1.el9.x86_64/bin/java
   2           /opt/jdk-16.0.2+7/bin/java

Enter to keep the current selection[+], or type selection number: 2

Enter the selection number for Java 16 (e.g., 2) and press Enter.

Repeat for javac (the compiler):

sudo update-alternatives --config javac

5. Verify Java 16 Installation#

Confirm Java 16 is installed correctly by checking the version:

java -version

You should see output like:

openjdk version "16.0.2" 2021-07-20
OpenJDK Runtime Environment Temurin-16.0.2+7 (build 16.0.2+7)
OpenJDK 64-Bit Server VM Temurin-16.0.2+7 (build 16.0.2+7, mixed mode, sharing)

To verify the compiler (javac):

javac -version

Output:

javac 16.0.2

6. Configure Environment Variables (JAVA_HOME)#

Many applications (e.g., Tomcat, Maven, Gradle) require the JAVA_HOME environment variable to locate the Java installation. Here’s how to set it system-wide (for all users):

  1. Find the Java Home Directory: For DNF-installed Java 16 (Temurin), the path is usually:

    /usr/lib/jvm/temurin-16-jdk

    For manual installation, it’s:

    /opt/jdk-16.0.2+7
  2. Create a Environment Variable Script: Open a new file in /etc/profile.d/ (scripts here are sourced on login):

    sudo nano /etc/profile.d/java.sh
  3. Add the Following Lines (replace the path with your Java home):

    export JAVA_HOME=/opt/jdk-16.0.2+7
    export PATH=$JAVA_HOME/bin:$PATH
    • JAVA_HOME: Points to the Java installation directory.
    • PATH: Adds the Java bin directory to the system path (so you can run java/javac without full paths).
  4. Save and Exit (Ctrl+OEnterCtrl+X).

  5. Apply the Changes: Source the script to make the variables available in your current session:

    source /etc/profile.d/java.sh
  6. Verify the Variables:

    echo $JAVA_HOME
    echo $PATH

    You should see your Java home path and the bin directory in PATH.

7. Uninstall Java 16#

If you no longer need Java 16, uninstall it based on how you installed it:

A. Uninstall via DNF#

sudo dnf remove temurin-16-jdk -y

B. Uninstall Manual Installation#

  1. Remove the Java Directory:
    sudo rm -rf /opt/jdk-16.0.2+7
  2. Remove Alternatives Entries:
    sudo update-alternatives --remove java /opt/jdk-16.0.2+7/bin/java
    sudo update-alternatives --remove javac /opt/jdk-16.0.2+7/bin/javac
  3. Delete the Environment Variable Script (if created):
    sudo rm /etc/profile.d/java.sh

8. Troubleshooting Common Issues#

A. "java: command not found"#

  • Cause: The Java binary isn’t in your PATH, or alternatives aren’t configured.
  • Fix:
    1. Re-run the update-alternatives --install commands (Step 3.3).
    2. Verify JAVA_HOME and PATH are set correctly (Step 6.6).

B. Wrong Java Version Showing#

  • Cause: Another Java version is set as default.
  • Fix: Use sudo update-alternatives --config java to switch to Java 16 (Step 4).

C. GPG Key Errors During DNF Installation#

  • Cause: The Adoptium GPG key wasn’t imported correctly.
  • Fix: Re-import the key (Step 2.1) and refresh the cache:
    sudo rpm --import https://packages.adoptium.net/artifactory/api/gpg/key/public
    sudo dnf makecache

D. Permission Denied When Running Java#

  • Cause: The Java binary doesn’t have execute permissions.
  • Fix: Add execute permissions to the binary:
    sudo chmod +x /opt/jdk-16.0.2+7/bin/java

9. Conclusion#

You’ve successfully installed Java 16 on Rocky Linux or AlmaLinux! Whether you used DNF (for simplicity) or manual installation (for control), you now have a working Java 16 environment.

Key Notes:#

  • Java 16 is a Feature Release: It includes new features but is not LTS (supported until September 2021). For production, use Java 11 (LTS until 2026) or Java 17 (LTS until 2029).
  • Multiple Java Versions: Use update-alternatives to switch between versions easily.
  • Environment Variables: Always set JAVA_HOME for applications that require it.

If you have questions or run into issues, let me know in the comments!

References#

  1. Adoptium Repository Setup: https://adoptium.net/installation.html#rpm
  2. Rocky Linux Package Management: https://docs.rockylinux.org/guides/package_management/
  3. AlmaLinux Documentation: https://wiki.almalinux.org/
  4. OpenJDK 16 Release Notes: https://openjdk.org/projects/jdk/16/
  5. update-alternatives Man Page: https://linux.die.net/man/8/update-alternatives