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.

Two small things before you go

If something didn’t work on your setup, leave a comment and tell me which distro and version you’re on. I read every one, and most of these articles got better because a reader pointed out what was missing.

And if it did work, share it. TecMint has stayed free for 14 years because people pass these articles around, and that traffic is what pays for writing the next one.

Leave a comment
Get two free Linux courses

Learn the Linux basics with two free courses and join 34,000+ readers getting practical TecMint tutorials and selected Pro TecMint articles every week.

Pro TecMint
Get lifetime access for $99

If you want to go beyond the free tutorials, Pro gives you 18 courses, 5 ebooks, and every future course I add. One payment, lifetime access, no renewals.

James Kiarie
James Kiarie
Contributor
About the author

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.

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.