How to install and configure Java JDK 22 on Ubuntu 22.04 LTS | install java jdk 22 on Ubuntu Linux

3 min read 8 hours ago
Published on Nov 14, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial provides a step-by-step guide on how to install and configure Java JDK 22 on Ubuntu 22.04 LTS. Java is a widely used programming language, and having the JDK (Java Development Kit) installed is essential for developing Java applications. Following these steps, you will be able to set up Java JDK 22 on your Ubuntu system effectively.

Step 1: Download Java JDK 22

  1. Open your terminal.

  2. Use the following command to download the JDK 22 package:

    wget https://download.oracle.com/java/22/latest/jdk-22_linux-x64_bin.deb
    
  3. Confirm that the download is complete by checking your current directory for the .deb file.

Step 2: Install the JDK Package

  1. After the download, run the following command to install the downloaded package:

    sudo dpkg -i jdk-22_linux-x64_bin.deb
    
  2. If you encounter any dependency issues, run the following command to fix them:

    sudo apt-get install -f
    
  3. Verify the installation by checking the Java version:

    java -version
    

    You should see output indicating that Java JDK 22 is installed.

Step 3: Set Up Environment Variables

  1. Open the environment variables file with a text editor:

    sudo nano /etc/environment
    
  2. Add the following lines at the end of the file to set the JAVA_HOME variable:

    JAVA_HOME="/usr/lib/jvm/jdk-22"
    PATH="$PATH:$JAVA_HOME/bin"
    

    Make sure to adjust the JAVA_HOME path if your installation directory is different.

  3. Save and exit the editor (press CTRL + X, then Y, and Enter).

  4. Update the current session with the new environment variables by running:

    source /etc/environment
    
  5. To confirm that JAVA_HOME is set correctly, use:

    echo $JAVA_HOME
    

Step 4: Compile and Run a Sample Java Program

  1. Create a new Java file using a text editor:

    nano HelloWorld.java
    
  2. Copy and paste the following sample Java code into the file:

    class HelloWorld {
        public static void main(String args[]) {
            System.out.println("Welcome to r2schools");
        }
    }
    
  3. Save and exit the editor.

  4. Compile the Java program using:

    javac HelloWorld.java
    
  5. Run the compiled Java program:

    java HelloWorld
    

    You should see the output: Welcome to r2schools.

Conclusion

You have successfully installed and configured Java JDK 22 on Ubuntu 22.04 LTS. By downloading the JDK, installing it, setting up the necessary environment variables, and running a sample Java program, you are now ready to start developing Java applications. As a next step, consider exploring Java programming tutorials or developing your own projects using Java.