In this guide we are going to explore how to install Java Runtime Environment (JRE) and the Java Developer Kit (JDK) in Rocky Linux 9. This guide also works for RHEL 9/Alma Linux 9.
Java and the JVM (Java’s virtual machine) are required for many kinds of software, including Tomcat, Jetty, Glassfish, Cassandra and Jenkins.
Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java.
Also Check:
- How to install Java 17 On Rocky Linux 8/Centos 8
- How to install Java 17 in Debian 11
- How to install Java 17 in Fedora 35
- How to install Java 17 in Ubuntu 20.04
Prerequisites
- Up to date Rocky Linux 9/ Rhel 9/Alma Linux 9 system
- Root access to the server or user with sudo access
- Internet access from the server
Table of content
- Ensuring that the server is up to date
- Installing java
- Testing the installation
- Toggling multiple Java versions
- Configuring the Environmental Variable
Ensuring that the server is up to date
Before proceeding, let us ensure that our server is up to date and all the packages are the latest version. Use these commands to achieve this:
sudo dnf -y update
If there are packages to upgrade, the above comand may take a couple of minutes.
Let us also install some common packages that we might need. I use vim text editor, and wget to download packages please make sure they are installed using this command:
sudo dnf -y install vim wget
Installing Java
Use the following command to check whether java is installed in your sytem.
$ java -version
-bash: java: command not found
If you see java: command not found
then it means that java is not installed. If java is installed, that command will print out the java version
The openjdk version 11 is available in the default Rocky Linux 9 repositories. You can search with dnf:
$ sudo dnf search jdk | grep java-11
Last metadata expiration check: 0:40:10 ago on Mon Aug 1 19:19:40 2022.
java-11-openjdk.x86_64 : OpenJDK 11 Runtime Environment
java-11-openjdk-demo.x86_64 : OpenJDK 11 Demos
java-11-openjdk-devel.x86_64 : OpenJDK 11 Development Environment
java-11-openjdk-headless.x86_64 : OpenJDK 11 Headless Runtime Environment
java-11-openjdk-javadoc.x86_64 : OpenJDK 11 API documentation
java-11-openjdk-javadoc-zip.x86_64 : OpenJDK 11 API documentation compressed in a single archive
java-11-openjdk-jmods.x86_64 : JMods for OpenJDK 11
java-11-openjdk-src.x86_64 : OpenJDK 11 Source Bundle
java-11-openjdk-static-libs.x86_64 : OpenJDK 11 libraries for static linking
In our case we are interested with the jdk. Install it with this command:
sudo dnf install java-11-openjdk java-11-openjdk-devel
Type y
when promted to confirm then wait for installation to finish. You can confirm the installed java version with this command:
$ java -version
openjdk version "11.0.16" 2022-07-19 LTS
OpenJDK Runtime Environment (Red_Hat-11.0.16.0.8-1.el9_0) (build 11.0.16+8-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-11.0.16.0.8-1.el9_0) (build 11.0.16+8-LTS, mixed mode, sharing)
To verify if the JDK is installed properly, we will check the version of javac, which is the Java compiler.
$ javac -version javac 11.0.16
Testing the installation
Let us test that Java 11 is installed properly in this step by creating a simple script that prints out Java is installed properly
.
Create the directory and switch to it
mkdir simple-app
cd simple-app
I use vim to create and edit text files in my system. Use the text editor of your choice in this step. I will create a file Simple.java
using this command:
vim Simple.java
Add these content to the file
public class Simple{
public static void main(String[] args) {
System.out.println("Citizix - Java is installed properly");
}
}
Now that we have created our program, we need to compile the Java source code into bytecode (class file) using the javac compiler.
javac Simple.java
You will get a new file called Simple.class
, which is the compiled Java class file.
$ ls
Simple.class Simple.java
The command above will only compile the Java source code into bytecode. In order to run the program, we run the java command with the name of our class file as an argument.
java Simple
If everything works well, you’ll see a message “Citizix - Java is installed properly
” on the screen.
$ java Simple
Citizix - Java is installed properly
Configuring the Environmental Variable
Some Java applications require the JAVA_HOME environmental variable in order to run properly. Some programs are very specific in how they are executed. If JAVA_HOME isn’t set, you’ll get an error. Setting JAVA_HOME will prevent this problem from arising.
To check which Java installations and paths exist, use this command:
$ which java
/usr/bin/java
In my case, I get the Java installation binary path to be this /usr/bin/java
If you have multiple versions of Java, you can use the update-alternatives command to configure the desired version:
$ sudo update-alternatives --config java There is 1 program that provides 'java'. Selection Command ----------------------------------------------- *+ 1 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.16.0.8-1.el9_0.x86_64/bin/java) Enter to keep the current selection[+], or type selection number:
Conclusion
In this tutorial, you have learned how to install Java 11 on a Rocky Linux 9 system. If you need further information about the Java language, please refer to its official documentation.