How to install and configure Java JDK 22 on Ubuntu 22.04 LTS | install java jdk 22 on Ubuntu Linux
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
-
Open your terminal.
-
Use the following command to download the JDK 22 package:
wget https://download.oracle.com/java/22/latest/jdk-22_linux-x64_bin.deb
-
Confirm that the download is complete by checking your current directory for the
.deb
file.
Step 2: Install the JDK Package
-
After the download, run the following command to install the downloaded package:
sudo dpkg -i jdk-22_linux-x64_bin.deb
-
If you encounter any dependency issues, run the following command to fix them:
sudo apt-get install -f
-
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
-
Open the environment variables file with a text editor:
sudo nano /etc/environment
-
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.
-
Save and exit the editor (press
CTRL + X
, thenY
, andEnter
). -
Update the current session with the new environment variables by running:
source /etc/environment
-
To confirm that JAVA_HOME is set correctly, use:
echo $JAVA_HOME
Step 4: Compile and Run a Sample Java Program
-
Create a new Java file using a text editor:
nano HelloWorld.java
-
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"); } }
-
Save and exit the editor.
-
Compile the Java program using:
javac HelloWorld.java
-
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.