How To Install Java on Rocky Linux 8
In this tutorial, we will show you how to install Java on Rocky Linux 8. For those of you who didn’t know, Java is a high-level programming language designed to be portable and to have as few dependencies as possible to run on any system. The general purpose of the Java programming language is to let developers write programs or applications once, but the application itself can be run on any system across multiple operating systems. Java has been around since 1995. It was released by Sun Microsystems as a programming language for “smart” consumer devices, particularly those with multimedia capabilities.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step installation of Java on Rocky Linux. 8.
Prerequisites
- A server running one of the following operating systems: Rocky Linux 8.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Install Java on Rocky Linux 8
Step 1. The first step is to update your system to the latest version of the package list. To do so, run the following commands:
sudo dnf check-update sudo dnf update
Step 2. Installing Java on Rocky Linux 8.
By default, Java is not available on Rocky Linux 8. base repository. Now run the following command below to install the OpenJDK 11 to your system:
sudo dnf install java-11-openjdk
Use the following command to check whether Java is installed:
java -version
Step 3. Managing Multiple Versions of Java.
Now, you will learn how to switch to a different java version the Rocky Linux using the command ‘alternatives
‘:
sudo alternatives --config java
There 2 different Java versions that you just installed on top.
- Java 11 – installed from the Rocky Linux repository.
- Java 1.8 – installed from the Rocky Linux repository.
Type in a number and press Enter to choose the default Java version. For example, we’ll be choosing OpenJDK 11 as the default, so we pressed Enter on “2”.
Step 4. Testing the installation.
Now that you have Java, you can write a basic “Hello World” program, compile, and execute it using Java to test your installation.
mkdir sample-app cd sample-app
Next, create a new file with the name “helloworld.java
” inside of the newly-created directory:
sudo nano helloworld.java
Add the following file:
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World, idroot"); } }
Save and close the file, then compile the Java source code into bytecode (class file) using the javac compiler:
javac helloworld.java
Congratulations! You have successfully installed Java. Thanks for using this tutorial for installing Java on your Rocky Linux 8 system. For additional help or useful information, we recommend you check the official Java website.