1080*80 ad

Installing Java 11, 17, and 18 on Rocky Linux

How to Install Java on Rocky Linux: A Step-by-Step Guide (OpenJDK 11, 17 & 21)

Java remains a cornerstone of modern software development, powering everything from enterprise-level server applications to Android apps. For developers and system administrators using Rocky Linux, having a properly configured Java Development Kit (JDK) is an essential first step. This guide provides a clear, straightforward method for installing popular Long-Term Support (LTS) versions of OpenJDK on your Rocky Linux system.

We will cover the installation of OpenJDK 11, 17, and 21, explaining how to manage multiple versions and configure the necessary environment variables for your applications to run smoothly.

Prerequisites: Preparing Your System

Before we begin, ensure your system is ready. You will need:

  • A system running Rocky Linux.
  • Access to a user account with sudo or root privileges.

First, it’s always a best practice to update your system’s package repository to ensure you have the latest security patches and software versions. Open your terminal and run the following command:

sudo dnf update -y

Once the update is complete, your system is prepared for the Java installation.

Step 1: Installing a Specific OpenJDK Version

Rocky Linux’s default repositories provide access to several versions of OpenJDK, making the installation process simple and secure. It is highly recommended to install the JDK (Java Development Kit) rather than just the JRE (Java Runtime Environment), as the JDK includes the tools needed for compiling and debugging applications.

You can easily install the OpenJDK version that best suits your project’s requirements.

Installing OpenJDK 21 (Latest LTS)

OpenJDK 21 is the most recent Long-Term Support release, offering the latest features and performance improvements. To install it, use the following command:

sudo dnf install java-21-openjdk-devel

Installing OpenJDK 17 (Popular LTS)

OpenJDK 17 is another widely used LTS version, required by many modern frameworks and applications. To install it, run:

sudo dnf install java-17-openjdk-devel

Installing OpenJDK 11 (Legacy LTS)

For projects that require compatibility with older systems, OpenJDK 11 remains a stable and reliable choice. Use this command for installation:

sudo dnf install java-11-openjdk-devel

The dnf package manager will automatically handle all necessary dependencies.

Step 2: Verifying Your Java Installation

After the installation completes, you should verify that Java has been correctly installed and is accessible from the command line. To check the active Java version, run:

java -version

If you installed OpenJDK 17, for example, you should see output similar to this:

openjdk version "17.0.5" 2022-10-18
OpenJDK Runtime Environment (build 17.0.5+8-LTS)
OpenJDK 64-Bit Server VM (build 17.0.5+8-LTS, mixed mode, sharing)

This confirms that the JDK is successfully installed and ready to use.

Step 3: Managing Multiple Java Versions

It’s common for developers to need multiple Java versions installed on the same machine. Rocky Linux uses the alternatives utility to easily switch between different installed versions without changing system paths manually.

To see all installed Java versions and select the default one, run the interactive command:

sudo alternatives --config java

The system will present you with a list of installed Java executables.

There are 3 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
*+ 1           /usr/lib/jvm/java-17-openjdk-17.0.5.0.8-2.el9_0.x86_64/bin/java
   2           /usr/lib/jvm/java-11-openjdk-11.0.17.0.8-2.el9_0.x86_64/bin/java
   3           /usr/lib/jvm/java-21-openjdk-21.0.1.0.12-2.el9_0.x86_64/bin/java

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

Simply enter the selection number corresponding to the version you wish to make the system default and press Enter. You can run java -version again to confirm the change.

Step 4: Configuring the JAVA_HOME Environment Variable

Many Java-based applications, such as Maven, Gradle, and Tomcat, rely on the JAVA_HOME environment variable to locate the JDK installation directory. Setting this variable is crucial for application compatibility.

  1. Find the correct JDK path. Use the alternatives command again to identify the installation path of your desired version. For example, to find the path for OpenJDK 17, you would look at the output from the previous step.

  2. Set the variable system-wide. The best practice is to create a new script file in the /etc/profile.d/ directory. This ensures the variable is set for all users upon login.

    Create a file named java.sh:

    sudo nano /etc/profile.d/java.sh
    
  3. Add the export commands. Add the following lines to the file, replacing the path with the one that corresponds to your chosen JDK version.

    export JAVA_HOME="/usr/lib/jvm/java-17-openjdk-17.0.5.0.8-2.el9_0.x86_64"
    export PATH=$PATH:$JAVA_HOME/bin
    
  4. Activate the variable. To apply the changes immediately in your current session, use the source command. Otherwise, the new variable will be loaded the next time you log in.

    source /etc/profile.d/java.sh
    
  5. Verify the variable. Confirm that JAVA_HOME is set correctly by echoing its value:
    bash
    echo $JAVA_HOME

    The output should match the path you entered in the script file.

Actionable Security Tips

  • Always use official repositories: Installing Java via dnf from Rocky Linux’s official repositories ensures you receive timely security updates and stable, tested packages.
  • Keep your system updated: Regularly run sudo dnf update to patch any discovered vulnerabilities in your installed Java versions.
  • Install the minimal package necessary: For production servers that only run a Java application and don’t need compilation, consider installing the java-xx-openjdk-headless package. It excludes GUI libraries, reducing the system’s attack surface.

Source: https://kifarunix.com/install-java-11java-17java-18-on-rocky-linux/

900*80 ad

      1080*80 ad