How to Install Java on Ubuntu 22.04


Java programming language provides advanced features for software development. The developers are offered advanced features to build software including desktop applications and mobile applications.

Although there are other advanced programming languages available, Java is still a widely installed programming language worldwide. Everyone can freely install Java on their desired operating system, Windows and Linux.

Aiming to explore and guide you through the process of installing Java on Ubuntu 22.04, this article provides step-by-step instructions.

How to Install Java on Ubuntu 22.04?

In this first section, we will guide you through the complete process of installing Java on Ubuntu 22.04.

Step 1: Install Java Package

The complete Java package, and development tool kit, can be installed on Ubuntu using the following command:

sudo apt install -y default-jdk

The installation process has been completed without causing any errors.

Step 2: Update System Files

Always keep your operating system up to date to improve system performance by using the following command:

sudo apt update

Step 3: Check Java Version

This command will display the installed version of Java:

java --version

Step 4: Test Java via Terminal

To initiate the Java scripting, simply run the “jshell” from the terminal:

jshell

Type your Java code, for example:

System.out.println("Welcome to Java World!");

You can exit your Java shell using the command:

/exit

In the above script, you can see that codes are running properly.

How to Remove Java From Ubuntu 22.04?

In this section, you will learn about the removal of the Java package from your operating system such as Ubuntu 22.04.

Step 1: Display Installed Java Package

First thing first, we will search for the available Java package installed on the Ubuntu machine. The command given below will be used for the purpose:

dpkg -l | grep -E "^ii.*(jdk|jre)"

Based on the above screenshot, there are three package files associated with Java.

Step 2: Remove OpenJDK

Run the command from the Ubuntu CLI to remove OpenJDK, a Java Development Kit, from your operating system. The “*”, a wildcard, is used to match any package starting with “openjdk.” :

sudo apt remove -y openjdk*

The screenshot presented above shows that all the packages related to “openjdk” have been removed from your operating system.

Step 3: Remove Java Development Kit “JDK-20”

To remove a Java package called JDK-20 from your system, run the command provided below:

sudo apt remove -y jdk-20

The output shown above indicates that the “jdk-20” package is no longer installed on your Ubuntu system.

Step 4: Confirmation

The command below will check if the Java packages such as, “jdk” and “jre” have been completely removed from your system:

java --version

As shown above, no such file or directory is found associated with Java on Ubuntu 22.04.

Conclusion

Ubuntu users can easily install the Java package using “sudo apt install -y default-jdk”. The option “default-jdk” includes both the Java Development Kit (JDK) and Java Runtime Environment (JRE). For executing Java code, use the keyword “jshell” from the command line. In this article, you have improved your understanding of the installation, execution, and removal of Java packages from Ubuntu 22.04.

Print Friendly, PDF & Email
Categories
Tags