How to Install Java on Debian 12 Linux

In the software development industry, JAVA, a programming language, is known for its importance. This programming language is used to develop popular and scalable applications because of its extensive libraries. 

JAVA is used to develop both applications, mobile as well as computer. This guide will explain the installation method and the usage of JAVA on Debian 12. 

What Do You Know About the JAVA Programming Language?

JAVA programming language is developed and maintained by the Oracle Corporation. It is independent of the platform because of which it can be run on all the operating systems including Linux distributions and Windows. 

The key features of the JAVA programming language are:

  • The code of JAVA can be written on one OS and run on another OS
  • OOP is followed in JAVA coding using the classes, inheritance, and polymorphism
  • JAVA provides a wide range of libraries including built-in functions
  • JAVA is used in the development of mobile applications, web applications, scientific and data analysis

How to Install JAVA on Debian 12?

There are two different installation methods of JAVA on Debian 12:

  1. Using the repository package
  2. Using the Debian package

Both these methods are explained with a step-by-step guide. 

Method 1: Using the Repository Package

Follow the below instructions to install JAVA on Debian using its official repository.

Step 1: Update the Packages

First update all the packages of the Debian 12 repository:

$ sudo apt update
Install Java on Debian 12 Linux 1

Step 2: Find the JAVA Package

Display the version details of the JAVA package available in the default repository of Debian 12:

$ sudo apt show default-jdk
Install Java on Debian 12 Linux 2

JAVA 17 is available in the default repository of Debian 12. 

Step 3: Install JAVA on Debian 12

Use the “install” option of the apt package manager for the installation of JAVA 17:

$ sudo apt install default-jdk -y
Install Java on Debian 12 Linux 3

Step 4: Verify the Installation

To verify the installation of the JAVA 17, display the version of the installed package:

$ java --version
Install Java on Debian 12 Linux 4

How to Uninstall JAVA on Debian 12?

To uninstall the JAVA on Debian 12 with its installed libraries, use the “purge” option of the apt package manager:

$ sudo apt purge default-jdk -y
Install Java on Debian 12 Linux 5

Method 2: Using the Debian Package of JAVA

Another method of installing the JAVA on Debian 12 is using a Debian package and for this purpose, follow the steps mentioned below. 

Step 1: Download the Debian Package

First, download the Debian package of JAVA 17 from its official website using the wget command:

$ wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.deb
Install Java on Debian 12 Linux 6

Step 2: Install the Debian Package

Now use the “i” option of the dpkg package manager to install the downloaded Debian package of JAVA 17:

$ sudo dpkg -i jdk-17_linux-x64_bin.deb
Install Java on Debian 12 Linux 7

Step 3: Verify the Installation of JAVA 17

To verify the installation of the JAVA, display the version of the JAVA:

$ java --version
Install Java on Debian 12 Linux 8

How to Uninstall JAVA 17 on Debian 12?

To uninstall the JAVA 17 on Debian 12, use the “r” option of the dpkg package manager:

$ sudo dpkg -r jdk-17
Install Java on Debian 12 Linux 9

The package has been removed from Debian 12 successfully.

How can JAVA be used on Debian 12?

To use the JAVA on Debian 12, create a new file with the Java extension using the nano text editor:

$ nano itslinuxfoss_java.java
Install Java on Debian 12 Linux 10

Write the JAVA script to display the welcome note:

public class itslinuxfoss_java {  public static void main(String[] args) {    System.out.println("Welcome to itslinuxfoss!");  }}
Install Java on Debian 12 Linux 11

Exit the nano text editor by saving the file with the CTRL+S. Now compile the JAVA script by using the javac compiler:

$ javac itslinuxfoss_java.java
Install Java on Debian 12 Linux 12

Now, run the JAVA script by using the command:

$ java itslinuxfoss_java
Install Java on Debian 12 Linux 13

The JAVA script has been executed successfully. 

Conclusion

For the installation of the JAVA using the default repository, run the command “sudo apt install default-jdk -y”. Another method of installing JAVA on Debian 12 is by downloading its Debian package. In this post, both methods of installing the JAVA, using the default repository and Debian package,  on Debian 12 have been explained with the step-by-step guide.