In this guide we are going to explore how to install Java Runtime Environment (JRE) and the Java Developer Kit (JDK) in Rocky Linux 8. This guide also works for Centos 8/Rhel 8/Alma Linux 8
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/Alma Linux 9
- 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 8/Rhel 8/Alma Linux 8 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 available in the default Rocky Linux Repos is not up to date. Get the latest in this https://jdk.java.net/17/
Download with this curl command:
curl -LO https://download.java.net/java/GA/jdk17.0.1/2a2082e5a09d4267845be086888add4f/12/GPL/openjdk-17.0.1_linux-x64_bin.tar.gz
This is the output on my server:
$ curl -LO https://download.java.net/java/GA/jdk17.0.1/2a2082e5a09d4267845be086888add4f/12/GPL/openjdk-17.0.1_linux-x64_bin.tar.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 178M 100 178M 0 0 76.3M 0 0:00:02 0:00:02 --:--:-- 76.3M
The file will be downloaded to the current directory as
openjdk-17.0.1_linux-x64_bin.tar.gz
$ ls
openjdk-17.0.1_linux-x64_bin.tar.gz
Let’s extract the package with tar command
tar -xzf openjdk-17.0.1_linux-x64_bin.tar.gz
Now let us move the content that was extracted to the /opt
directory
sudo mv jdk-17.0.1/ /opt
Now that the content is extracted and added to the /opt
directory, we need to export Java home so the java apps know where the JDK installation is and to update the path to include executables in the java binary directory. That way we can use the java
command without specifying the full path.
Let us set these variables in the .bashrc
file. Open the file with your text editor, I am using vim:
vim ~/.bashrc
Now add these lines to set Java home and to include the executables binary directory
export JAVA_HOME=/opt/jdk-17.0.1
export PATH=$PATH:$JAVA_HOME/bin
With those set, you will have to logout then login or reload the login shell using this command:
source ~/.bashrc
Confirm that Java is now working fine by checking for the version:
$ java -version
openjdk version "17.0.1" 2021-10-19
OpenJDK Runtime Environment (build 17.0.1+12-39)
OpenJDK 64-Bit Server VM (build 17.0.1+12-39, 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 17.0.1
Testing the installation
Let us test that Java 17 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
/opt/jdk-17.0.1/bin/java
In my case, I get the Java installation binary path to be this /opt/jdk-17.0.1/bin/java
We are going to set this path in an enviroment file /etc/environment
where all the apps can access.
Open the file with vim:
sudo vim /etc/environment
Now we need to add a line exporting the Java home to the file like this export JAVA_HOME=...
making the value to be the path we got for our Java installation:
export JAVA_HOME=/opt/jdk-17.0.1/bin/java
This file will set the JAVA_HOME path of OpenJDK 17 as environmental variables for all users in the system.
To apply the change, run the following command.
source /etc/environment
To verify that the path variable has been applied, run the following command.
echo $JAVA_HOME
The path should be set to the path of OpenJDK 17, which you have copied above. This is the value in my case:
$ echo $JAVA_HOME
/opt/jdk-17.0.1/bin/java
From now on, you don’t have to set the JAVA_HOME path every time when using Java programs. Also, if you want to use other OpenJDK versions instead of 17, adapt the path accordingly.
Conclusion
In this tutorial, you have learned how to install Java 17 on a Rocky Linux 8/Alma Linux 8 system. If you need further information about the Java language, please refer to its official documentation.