How To Install Java on Debian 11

Java is an open-source programming language released in 1995 by Sun Microsystems.  Many developers love to develop in Java as it can be used to create Desktop applications as well as mobile apps. To talk in numbers, there are about 9 million developers out there who prefer the Java programming language to develop and create applications. Not just this, there are millions of Java apps that require you to have Java on your system otherwise those apps won’t function.

As mentioned earlier, Java is an important tool to have on our system as Java will aid us in running Java applications and we can also develop/create multiple apps per our need. It is for this reason that we must have Java installed on our system.

This article will walk you through each step to install/uninstall Java on Debian 11. Not only this, but we will also teach you how to create your first program in Java. Amazing right? Let’s get started then.

Installation of Java

Debian 11 Bullseye has the latest and the stable Java version OpenJDK 11 as well as the OpenJDK 17. It is on your preference whichever you choose between them.

There are also two Java platforms that we can use:

  • Java Runtime Environment(JRE)
  • Java Development Kit (JDK)

The purpose of JRE is to help us in running all the applications that are available/developed in Java and JDK helps in developing/creating Java applications.

1- Java Open JDK11

Let’s install the latest version of Java Open JDK 11 on Debian 11. To achieve this type or copy the following command in your Debian 11 terminal:

$ sudo apt install default-jre default-jdk 

2- Java OpenJDK 17

As mentioned above, it is our preference which JDK we want to install. If we want to go for the latest version then it is the Java Open JDK17. To install Open JDK17 type or copy the following command in your terminal:

$ sudo apt install openjdk-17-jre openjdk-17-jdk

It is also worth mentioning that you can have both of these versions on your system.

Once we are done with the installation let us verify that the installation was successful or not. For this purpose we have to use the following command:

$ java --version

Let us check the JDK version we installed on our system/Debian11. Type the following command on your terminal:

$ javac -version

First Program in Java

Now that we have installed Java, let us write a program and execute it in Java. For this purpose, we have to create a file or open an existing file. Type the following command in your terminal to create a file:

$ sudo nano itslinuxfoss.java

This will open or create the file with the name of itslinuxfoss.

Once the file is opened, type or copy the following lines in your editor:

class myProgram {

public static void main(String args[]){

        System.out.println("Hello World! Its us from linux foss");
     }
}

Now press CTRL+S to save the file and then press CTRL+X to exit the file.

To compile the above program, type or copy the following command in your terminal:

$ javac itslinuxfoss.java

Now that we have compiled our file itslinuxfoss.java, we can look at the class created with the following command:

$ ls

Let us execute the compiled program so that we can see the output. Type the following command in your terminal:

$ java myProgram

Congratulations! You have created your first program in Java.

Uninstall Java

You will get to a point where you will want to uninstall Java. It may be for various reasons e.g you don’t need java anymore or you need space in your system. If you want to uninstall Java, Don’t worry we got your back.

The method for uninstalling is pretty simple as we can uninstall any Java Package like any package that is installed with apt. The first command to uninstall the Java package is to type the following command:

$ sudo apt remove default-jdk

Conclusion

Java has been a popular programming language because of its flexibility as it can be used to create/develop applications in mobiles as well as desktop applications. Developers love to code in Java and create different kinds of applications using Java.

In this article, we walked you step by step in installing Java and creating your first-ever program in Java. We also mentioned how to uninstall Java in this article.