Table of Contents#
- Prerequisites
- 1. Update System Packages
- 2. Install Java 16 via DNF (Recommended)
- 3. Manual Installation of Java 16
- 4. Set Default Java Version
- 5. Verify Java 16 Installation
- 6. Configure Environment Variables (JAVA_HOME)
- 7. Uninstall Java 16
- 8. Troubleshooting Common Issues
- 9. Conclusion
- References
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 -yThis step prevents conflicts and ensures compatibility with new software installations.
2. Install Java 16 via DNF (Recommended)#
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#
-
Import the Adoptium GPG Key (to verify package integrity):
sudo rpm --import https://packages.adoptium.net/artifactory/api/gpg/key/public -
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 -
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/8withcentos/9in thebaseurl.
- For Rocky Linux 9/AlmaLinux 9, replace
-
Save and Exit: Press
Ctrl+O→Enter→Ctrl+X. -
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 -yIf 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#
-
Go to the Adoptium Temurin 16 Releases page:
https://adoptium.net/temurin/releases/?version=16 -
Select:
- Operating System: Linux
- Architecture: x64 (most modern systems)
- Package Type: Tar.gz
- JVM Variant: HotSpot (default, optimized for performance)
-
Copy the download link for the latest version (e.g.,
OpenJDK16U-jdk_x64_linux_hotspot_16.0.2_7.tar.gz). -
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#
-
Extract the Tarball:
tar -xzf OpenJDK16U-jdk_x64_linux_hotspot_16.0.2_7.tar.gzThis creates a directory like
jdk-16.0.2+7. -
Move the Directory to /opt:
/optis the standard location for third-party software on Linux. Usesudoto 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:
-
Register the
javaCommand:sudo update-alternatives --install /usr/bin/java java /opt/jdk-16.0.2+7/bin/java 1602/usr/bin/java: The system-widejavaexecutable./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).
-
Register the
javacCommand (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 javaYou’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 javac5. Verify Java 16 Installation#
Confirm Java 16 is installed correctly by checking the version:
java -versionYou 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 -versionOutput:
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):
-
Find the Java Home Directory: For DNF-installed Java 16 (Temurin), the path is usually:
/usr/lib/jvm/temurin-16-jdkFor manual installation, it’s:
/opt/jdk-16.0.2+7 -
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 -
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:$PATHJAVA_HOME: Points to the Java installation directory.PATH: Adds the Javabindirectory to the system path (so you can runjava/javacwithout full paths).
-
Save and Exit (
Ctrl+O→Enter→Ctrl+X). -
Apply the Changes: Source the script to make the variables available in your current session:
source /etc/profile.d/java.sh -
Verify the Variables:
echo $JAVA_HOME echo $PATHYou should see your Java home path and the
bindirectory inPATH.
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 -yB. Uninstall Manual Installation#
- Remove the Java Directory:
sudo rm -rf /opt/jdk-16.0.2+7 - 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 - 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:
- Re-run the
update-alternatives --installcommands (Step 3.3). - Verify
JAVA_HOMEandPATHare set correctly (Step 6.6).
- Re-run the
B. Wrong Java Version Showing#
- Cause: Another Java version is set as default.
- Fix: Use
sudo update-alternatives --config javato 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-alternativesto switch between versions easily. - Environment Variables: Always set
JAVA_HOMEfor applications that require it.
If you have questions or run into issues, let me know in the comments!
References#
- Adoptium Repository Setup: https://adoptium.net/installation.html#rpm
- Rocky Linux Package Management: https://docs.rockylinux.org/guides/package_management/
- AlmaLinux Documentation: https://wiki.almalinux.org/
- OpenJDK 16 Release Notes: https://openjdk.org/projects/jdk/16/
- update-alternatives Man Page: https://linux.die.net/man/8/update-alternatives