How to Install and Use Java JDK on Debian 11

JAVA JDK

What is Java?

Java is one of the most popular programming languages. Java is portable, so you can make your code run on different types of computers without having to rewrite it. It’s also object-oriented, which means that it enables programmers to write reusable software components.

What Does Java do?

The main goal of Java is to make programming more accessible and easier for developers. For instance, it supports all major platforms (Windows, Mac OS X, and Linux) and has a simple development environment. Programmers can write once and run anywhere since its bytecode compiling system makes the machine code platform agnostic.

To understand what this means: think of your operating systems like English and your processor like Spanish. You can translate from English to Spanish and vice versa by writing down the words in both languages; however, you could never go from Spanish to English without further instruction because they’re different languages.

Similarly, bytecode compiling is like translating between English and Spanish; you can translate from English (our source code) to the specific language your operating system uses (machine code) with bytecode compiling.

Java also simplifies development by including most of the basic programming elements developers need. For example, it provides primitive data types like Boolean, String, Integer, etc. It also has support for classes, which are templates that help programmers create new objects when writing software.

It is important to understand what these terms mean when discussing Java:

  • Byte Code – A set of instructions created by a compiler.
  • Classes – Templates that help programmers create new objects when writing software.
  • Primitive Data Types – The most basic programming elements, including Boolean, String, Integer, etc.

When Will I Have to Get Java?

You’ll need to get Java when you want your computer program to do more than just the simple processing possible with a scripting language. Like any programming language, however, debugging programs written in Java can be tricky! It’s best if you learn another programming language first so that you have a grounding in how programming languages work. You should also know something about Object Oriented Programming (OOP). This concept was invented for use with Java, and without OOP knowledge, it could be hard to understand how Java programs work.

Is There Anything Else I Need to Know About Java?

Yes! You’ll have to get an Integrated Development Environment (IDE) which you can use to write, test, and run Java programs. There are many available, but the two most popular are Eclipse and IntelliJ IDEA – both of these are free. If you choose not to use them, however, make sure you find something that lets you compile your code into an executable file before running it.

These often include a console window that will let you see text output from your program. This is useful for debugging purposes since it’s through here that information about errors in your code is usually displayed.

Before we begin, please note that there are two different implementations of Java: OpenJDK and Oracle Java. The latter has a few additional features that are only available to commercial users.

The applications you wish to run and your operating system determines which Java package you need to install. We should stick to the default OpenJDK version, which is available on macOS and most Linux operating systems.

As you can see, there are many different types of Java applications that have different requirements. So, if you haven’t already, consult the application’s documentation.

In this tutorial, we will show you how to install Java on Debian 11. This tutorial covers how to install OpenJDK and its corresponding JRE and JDK.

This can be a tricky and confusing process for beginners, but with our help, it is simplified into a very easy and straightforward process. Let’s get started!

Prerequisites

  • A server running Debian 11 operating system.
  • A root password is required during the installation process, so you should have one set before starting.
  • System requirements: Minimum 250 MB of disk space. Recommended is 1 GB of RAM or more.

Updating the System

To keep your system secure, it’s important to update regularly. In Linux systems, updates are composed of packages that can be installed from a catalog of tools called a “package manager.” It is the responsibility of the system administrator to monitor the package manager and install any important updates as they become available. This ensures that the system has all the latest security patches installed.

Debian 11 uses apt as its package manager, which can be used to update the system with this command:

sudo apt-get update && sudo apt-get dist-upgrade -y

Installing Java

The default Java development and runtime in Debian 11 is OpenJDK. OpenJDK is available from Debian 11’s main repository. So that you can install Java with the APT command as follows. You need to run the command as a user with sudo privileges or root to install OpenJDK.

sudo apt-get install default-jdk -y

This command will automatically install the Java Runtime Environment (JRE). Java Runtime Environment (JRE) is software that allows you to run Java applications.

It includes the libraries, shell scripts, and browser plugins needed to run Java applets and applications.

To create and compile Java applications, you’ll also need the JDK (Java Development Kit).. This tool includes the complete libraries, tools, and documentation necessary to build, debug, and run custom JAR files. So if you want to develop (compile and build) Java programs in the Debian 11 operating system, then you will need to install the JDK package also by running the command below.

sudo apt install default-jdk

You should check if the installation was successful by running this command below.

java -version

You also may need to check if the JDK has been installed correctly by checking the version of javac, the Java compiler.

javac -version

You will get the following output.

Running Multiple Java Installations

You can have multiple Java installations on your server. This is useful in development environments when you might need different versions to run applications or libraries.

You can configure which version of Java you want to use by default by running this command below.

sudo update-alternatives --config java

This command opens the interactive configuration screen for Java, as shown below. Here you can set your installation preference by selecting the number associated with it. Or press Enter to keep the default.

Similarly, you can do the same for the javac compiler.

sudo update-alternatives --config javac

You will get the following output.

Testing the Java Installation

Now that Java is installed, you can test it by writing a small Java program, compiling it, and then executing it.

First, create a new directory named java_testing_project in your home directory to keep all your Java files and move into it by running the command below.

cd && mkdir java_testing_project && cd java_testing_project

Next, create a new file named HelloWorld.java using the nano text editor with sudo privileges. You can use any name you want for this file.

sudo nano HelloWorld.java

Populate the file with the following code. Start a new line after every line of code. Make sure not to include any blank lines between them.

public class HelloWorld {
  public static void main(String[] args) {
    System.out.println(“Hello OSNote”);
  }
}

Where:

  • public class HelloWorld {: This creates a new “HelloWorld” class.
  • public static void main(String[] args) {: This is the main method for this program. It means that when the program starts, the code inside of it will be run first.
  • System.out.println(“Hello OSNote”): This line prints “Hello OSNote” in the console.
  • } This closes the main method and thus ends the program.

When you are done, save the file with Ctrl + O, then press Enter to confirm the filename. Pressing Ctrl + X to exit the nano editor.

Now that the file has been saved, it is time to compile the program. To do this, you will need to run the javac command with the file name as an argument as follows.

javac HelloWorld.java

This command will not produce any output on the console. This command will generate a new file called “HelloWorld.class” in the same directory that the Java file was in. If you list the directory contents with the ls command, you can see this new file.

ls

Finally, you are ready to run your Java program by running the java command with the class file name as an argument.

java HelloWorld

If you encounter an error, ensure you typed out both commands correctly and did not forget any arguments.

Popular Linux JAVA Applications

A popular web-based Java application on Linux is XWiki. XWiki is software to set up Wiki pages (Like Wikipedia) easily and provides advanced permissions management with loadable core modules, and it can act as an application wiki. There are plenty of XWiki extensions available, XWiki extensions ranging from applications to macros, skins, plugins, structured data, and themes.

XWiki

Conclusion

In this tutorial, you have learned how to install Java on Debian 11. You also learned how to manage multiple versions of Java on Debian 11. Furthermore, you learned how to test your Java installation by creating a simple application in Java and compiling it. For feedback, questions, or concerns, please leave a comment below.

Published
Categorized as Debian