How to Install Java on Debian 12

In this blog post, we will explain how to install Java on Debian 12 step-by-step.

Java is a high-level, object-oriented programming language renowned for its platform independence. Developed by Sun Microsystems (now owned by Oracle), Java allows developers to write code once and run it anywhere. This “write once, run anywhere” (WORA) capability stems from its ability to compile code into an intermediate form called bytecode, which can be executed on any device equipped with the Java Virtual Machine (JVM).

Prerequisites

  • Pre-Install Debian 12
  • Sudo user with admin rights
  • Internet Connectivity

1. Update Apt Repositories

Open the terminal and run following apt command to update your sytstem’s apt repositories.

$ sudo apt update

Apt-Update-Repository-Debian12

2. Install Java on Debian 12

Debian 12 offers multiple Java implementations, including OpenJDK and Oracle JDK. So, to install OpenJDK, the open-source alternative, run following command.

$ sudo apt install default-jdk -y

Install Java on Debian 12

This command will install the default version of OpenJDK available in the Debian repository. If you need a specific version, you can adjust the package name accordingly (e.g., openjdk-11-jdk).

Post installation verify its version, run

$ java --version

Check-Java-Version-on-Debian12

Output above shows that we have installed Java version 17.0.9

Install Java (Oracle JDK) on Debian 12 via Debian Package

In case, you want to install latest version of version Java (oracle JDK) then download its Debian package from its official web site or use below wget command from the terminal.

Note: At the time of writing this guide, latest version of Oracle JDK was 21.

$ wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.deb

Download-Oracle-JDK-on-Debian12

Execute the following dpkg command to install above downloaded oracle jdk Debian package.

$ sudo dpkg -i jdk-21_linux-x64_bin.deb

Install-Oracle-JDK-Debian-Package-on-Debian12

Verify the Java version,

$ java --version

Oracle-JDK-Java-Version-Debian12

Great, output above confirms that Java version 21.0.2 is installed on your system.

3. Set Java Environment Variables

To ensure that your system recognizes Java, you need to set the Java environment variables. First find the path where java is installed, execute beneath command.

$ ls -ld /usr/lib/jvm/jdk*

JDK-Path-on-Debian12

Next, edit /etc/environment file using a text editor and add the following lines,

$ sudo vi /etc/environment

export JAVA_HOME="/usr/lib/jvm/jdk-21-oracle-x64"
export PATH="$PATH:${JAVA_HOME}/bin"

save and close the file.

Now source the environment file and then check Java home,

$ source  /etc/environment
$ echo $JAVA_HOME
/usr/lib/jvm/jdk-21-oracle-x64
$

4. Test Java Installation

To test Java installation, let’s create a sample program which will print “Hello LinuxBuzz”. Create a file add following content to it.

$ vi HelloLinuxBuzz.java

public class HelloLinuxBuzz {
    public static void main(String[] args) {
        System.out.println("Hello, LinuxBuzz!");
    }
}

Save and exit the file.

Compile the Java code using the following command:

$ javac HelloLinuxBuzz.java

Execute the compiled program:

$ java HelloLinuxBuzz
Hello, LinuxBuzz!
$

If all goes well, you should see “Hello, LinuxBuzz!” printed to the terminal.

Compile-Run-Java-Code-Debian12

5. Uninstall Java on Debian 12

To uninstall or remove openjdk , run following command

$ sudo apt purge default-jdk -y

To uninstall oracle JDK, run beneath command.

$ sudo dpkg -r jdk-{package-version}

Example:

$ sudo dpkg -r jdk-21

Uninstall-Java-Debian12

That’s all from this post, we hope you have found it informative and useful. Feel free to post your queries and feedback in below comments section.

Leave a Comment

16 − 4 =