WindowsMacSoftwareSettingsSecurityProductivityLinuxAndroidPerformanceConfigurationApple All

How to Install Java on Debian

Edited 4 days ago by ExtremeHow Editorial Team

DebianJavaSoftware InstallationDevelopmentCLILinuxOpen SourceSystem AdministrationProgrammingIT

How to Install Java on Debian

This content is available in 7 different language

Java is a popular programming language used in a wide range of applications, from web development to desktop applications. If you are using the Debian operating system and want to install Java, this detailed guide will help you with the process. We will cover the different versions of Java, how to select the right version for your needs, and the detailed steps to install Java on your Debian system. This guide is structured to provide you with understandable, detailed information on how to install Java, regardless of your previous experience with command-line interfaces or software installations.

1. Understanding Java versions and packages

Java is available in two different versions, let's understand their difference:

For our guide, we'll focus on installing OpenJDK as it's fully compatible and integrated with Debian's package management system. However, we'll also briefly explain how to install Oracle Java if that better aligns with your project's needs.

2. Checking the installed Java versions

Before proceeding with the new Java installation, it is useful to check if Java is already installed on your system. You can do this by opening a terminal and typing:

java -version

If Java is installed, this command will return the current version number. If it is not recognized, you will see a message indicating that Java is not installed.

3. Installing OpenJDK

Debian repositories usually contain OpenJDK. Let's start installing it:

  1. Update your package index: It is always recommended to update your system before installing new packages to ensure you have the latest software versions and dependencies. Type the following command:
    sudo apt update
  2. Install OpenJDK: You can install the default OpenJDK available on Debian by typing the following:
    sudo apt install default-jdk
    This command will install the default Java Development Kit, which includes the Java Runtime Environment (JRE) required to run Java applications. Optionally, you can specify a different version:
    sudo apt install openjdk-<version>-jdk
    Replace <version> with the version number you want to install, such as openjdk-8-jdk or openjdk-11-jdk.
  3. Confirm the installation: Confirm the installation of Java by running again:
    java -version
    The terminal should now display the installed version of OpenJDK.

4. Installing Oracle Java

If you need to use Oracle Java instead of OpenJDK, here is the method for manual installation:

  1. Download Oracle JDK: Visit Oracle Java download page and download the tar.gz file for Linux. Make sure to select the version as per your project requirement.
  2. Extract the package: Use tar command to extract the downloaded package. Go to your downloads directory (or wherever you downloaded the package) and run:
    tar -xzf jdk-<version>-linux-x64_bin.tar.gz
    Replace jdk-<version>-linux-x64_bin.tar.gz with the actual downloaded package name.
  3. Move the JDK to /opt: This location is traditionally used to install third-party software:
    sudo mv jdk-<version> /opt/
  4. Set environment variables: You will need to set Java environment variables such as JAVA_HOME and PATH. Open the file /etc/profile in a text editor (you may need superuser privileges):
    sudo nano /etc/profile
    Add the following lines to the file:
    export JAVA_HOME=/opt/jdk-<version> export PATH=\$PATH:\$JAVA_HOME/bin
    Save and close the file, then apply the changes:
    source /etc/profile
  5. Verify installation: Check the Java version to make sure Oracle Java is installed:
    java -version

5. Managing multiple Java versions

Debian allows you to install multiple Java versions and easily switch between them with update-alternatives utility. To configure the default Java version, follow these steps:

  1. Configure options: First, list all the Java options available on your system:
    sudo update-alternatives --config java
  2. Choose default version: Follow the prompts and select a number to choose your default Java version Enter after typing the number. This configuration also applies to other Java commands such as javac, javadoc, and keytool.

6. Testing the Java installation

To make sure that Java is installed and working correctly, you can write a simple Java program. Use a text editor to create a file named HelloWorld.java that has the following content:

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

Use the following to compile the Java program:

javac HelloWorld.java

If compilation is successful, run the program:

java HelloWorld

The output should be Hello, World! which confirms that your Java installation is successful.

7. Keeping Java updated

Keeping your Java installation up to date is important for security and functionality. For OpenJDK updates, you can rely on the regular Debian update process:

sudo apt update sudo apt upgrade

These commands will update all existing packages including Java.

8. Conclusion

This guide walks you through installing Java on a Debian system, including both OpenJDK and Oracle Java. We discussed how to verify the installation, how to manage multiple Java versions, and how to test your installation with a simple Java program. Additionally, we explained how to keep your Java installation updated to ensure security and performance. By following this guide, you are well prepared to harness the potential of Java on your Debian machine.

If you find anything wrong with the article content, you can


Comments