How to Install Java 16 in Rocky Linux and AlmaLinux

Java is a cross-platform, object-oriented, and multipurpose programming language that is primarily used for creating mobile, web, and cloud applications. Additionally, you can use Java to create games, chatbots, enterprise applications, and a whole lot more.

To develop Java applications, you need to install an IDE (Integrated Development Environment). IntelliJ IDEA is a perfect example of an IDE that is designed specifically for the development of Java applications. However, you need to have Java installed beforehand. This can be provided by either the OpenJDK (Open Java Development Kit) or Oracle JDK (Oracle Development Kit).

[ You might also like: 27 Best IDEs for C/C++ Programming or Source Code Editors on Linux ]

OpenJDK is an open-source implementation of the Java SE. It’s a development environment that was initially created by Sun Microsystems and is currently sponsored and maintained by Oracle. OpenJDK constitutes the Java compiler, Java Runtime Environment (JRE), Java Virtual Machine (JVM), and Java class library.

At the time of writing this guide, the latest version of Java is Java 16, which is provided by OpenJDK 16. Join us as we examine how you can install Java 16 on Rocky Linux 8 (also works on AlmaLinux 8).

Installing Java (OpenJDK) in Rocky Linux

To get started, we need to confirm that Java is not yet installed by running the following command.

$ java --version

bash: java: command not found...

Next, we are going to download the OpenJDK 16 binary file which comes as a tarball file using the following curl command.

$ curl  -O https://download.java.net/java/GA/jdk16.0.2/d4a915d82b4c4fbb9bde534da945d746/7/GPL/openjdk-16.0.2_linux-x64_bin.tar.gz

Once the download is complete, extract the compressed binary file.

$ tar -xvf openjdk-16.0.2_linux-x64_bin.tar.gz

Then move the decompressed folder to the /opt directory as shown.

$ sudo mv jdk-16.0.2 /opt

Thereafter, set the environment variables as shown.

$ export JAVA_HOME=/opt/jdk-16.0.2
$ export PATH=$PATH:$JAVA_HOME/bin

Java is now installed. To confirm the version installed, run the following commands:

$ echo $JAVA_HOME
$ java --version
Check Java Version in Rocky Linux
Check Java Version in Rocky Linux

Testing Java (OpenJDK) in Rocky Linux

To test if Java was correctly installed and working, we will code a simple Java program that adds two integers as follows.

$ sudo vim Hello.java

Paste the following lines of code and save the file.

public class Hello {

    public static void main(String[] args) {
        // Adds two numbers
        int x = 45;
        int y = 100;
        int z = x + y;
        System.out.println("Hello, the sum of the two numbers is: " +z);
    }

}

Compile the Java code;

$ javac Hello.java

Then run the Java code

$ java Hello
Testing Java in Rocky Linux
Testing Java in Rocky Linux

Great, all seems fine. We have successfully installed OpenJDK 16 and tested it by compiling and running a simple Java program in Rocky Linux.

James Kiarie
This is James, a certified Linux administrator and a tech enthusiast who loves keeping in touch with emerging trends in the tech world. When I'm not running commands on the terminal, I'm taking listening to some cool music. taking a casual stroll or watching a nice movie.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.