Website Vidya
Cloud & DevOpsStep-by-Step Guide

How to Install Java on Ubuntu 24.04

Learn how to install java on ubuntu 24.04 with OpenJDK 21 LTS and 17 LTS, manage multiple versions with update-alternatives, and configure JAVA_HOME.

RR
Rajesh RayLead Systems Educator
Published:
5 min read
PDF Cheat Sheet
How to Install Java on Ubuntu 24.04
AI Executive SummaryWebsiteVidya AI

5 min read

Learn how to install java on ubuntu 24.04 with OpenJDK 21 LTS and 17 LTS, manage multiple versions with update-alternatives, and configure JAVA_HOME.

Key Highlights
  • Prerequisites and Supported Releases
  • JDK vs JRE: Choosing the Right Package
  • Step 1: Installing OpenJDK 21 LTS (Default Java)
Grounded in full engineering runbookWebsiteVidya AI Engine

Java remains one of the world's most ubiquitous programming languages and enterprise runtime platforms, powering big data systems, cloud microservices, Android development, and web frameworks.

Whether you are deploying Spring Boot microservices, running Elasticsearch clusters, hosting Jenkins automation servers, or developing enterprise applications, having a properly configured Java development kit is indispensable.

Ubuntu 24.04 LTS includes OpenJDK 21 (the current Long Term Support release) and OpenJDK 17 (the previous LTS release) natively in its official package archives.

In this guide, we walk through how to install java on ubuntu 24.04, choose between JRE and JDK packages, manage multiple coexisting Java versions, and configure the JAVA_HOME environment variable.

Advertisement

Prerequisites and Supported Releases

This guide is thoroughly verified on Ubuntu 24.04 LTS (Noble Numbat) & Ubuntu 22.04 LTS. The instructions apply cleanly to cloud instances (AWS EC2, Google Compute Engine, DigitalOcean, Hetzner), bare-metal servers, and local virtual machines.

Before proceeding, ensure you have the following requirements satisfied:

  • A server or workstation running Ubuntu 24.04 LTS or Ubuntu 22.04 LTS.
  • A non-root user account with sudo administrative privileges.
  • Access to a bash terminal shell.
  • At least 1 GB of available disk space.

JDK vs JRE: Choosing the Right Package

Before installing Java, determine whether your system requires the JRE (Java Runtime Environment) or JDK (Java Development Kit):

Advertisement

Review this architectural comparison to select the appropriate package for your server:

ComponentIncluded ToolsIdeal TargetAPT Package Name
JRE (Java Runtime Environment)Java Virtual Machine (JVM), core librariesProduction servers that only execute pre-compiled .jar filesopenjdk-21-jre / openjdk-21-jre-headless
JDK (Java Development Kit)Everything in JRE plus javac compiler, debugger, javadoc, jar packagerDeveloper workstations and build/CI servers (Jenkins, Maven, Gradle)openjdk-21-jdk / openjdk-21-jdk-headless

Step 1: Installing OpenJDK 21 LTS (Default Java)

OpenJDK 21 is the default Java runtime in Ubuntu 24.04 LTS, offering 5 years of upstream support with advanced features like virtual threads and patterned switch expressions.

First, update your local APT repository index:

Install the full OpenJDK 21 Development Kit:

Advertisement

Verify the installation by checking the runtime version and compiler:

# Refresh package index:
sudo apt update

# Install OpenJDK 21 JDK:
sudo apt install -y openjdk-21-jdk

# Verify runtime version:
java -version
# Expected output:
# openjdk version "21.0.4" 2024-07-16
# OpenJDK Runtime Environment (build 21.0.4+7-Ubuntu-1ubuntu224.04)
# OpenJDK 64-Bit Server VM (build 21.0.4+7-Ubuntu-1ubuntu224.04, mixed mode, sharing)

# Verify compiler version:
javac -version
# Expected output: javac 21.0.4

Step 2: Installing OpenJDK 17 LTS (Legacy Compatibility)

Many existing enterprise applications, Apache Spark jobs, or legacy Gradle builds still require OpenJDK 17 LTS.

You can safely install OpenJDK 17 alongside OpenJDK 21 without conflicts on Ubuntu 24.04:

Install the OpenJDK 17 package using apt:

# Install OpenJDK 17 JDK:
sudo apt install -y openjdk-17-jdk

# Verify OpenJDK 17 binary presence on disk:
ls -d /usr/lib/jvm/java-17-openjdk-amd64
# Output: /usr/lib/jvm/java-17-openjdk-amd64

Step 3: Managing Multiple Java Versions with update-alternatives

When multiple versions of Java are installed, Ubuntu uses the update-alternatives system to manage symbolic links pointing to the default binary.

Run the interactive configuration utility to view all installed Java runtimes and select your preferred default version:

Type the selection number corresponding to the version you wish to make active, then press Enter:

# Configure default Java runtime:
sudo update-alternatives --config java
# Output:
# There are 2 choices for the alternative java (providing /usr/bin/java).
#
#   Selection    Path                                            Priority   Status
# ------------------------------------------------------------
# * 0            /usr/lib/jvm/java-21-openjdk-amd64/bin/java      2111      auto mode
#   1            /usr/lib/jvm/java-17-openjdk-amd64/bin/java      1711      manual mode
#   2            /usr/lib/jvm/java-21-openjdk-amd64/bin/java      2111      manual mode
#
# Press <enter> to keep the current choice[*], or type selection number: 1

# Configure matching Java compiler (javac):
sudo update-alternatives --config javac

Step 4: Setting the JAVA_HOME Environment Variable

Enterprise development tools like Apache Maven, Gradle, Apache Kafka, and Tomcat require the JAVA_HOME environment variable to identify the JVM installation path.

Identify the exact path of your desired Java installation:

Set JAVA_HOME system-wide by appending the declaration to /etc/environment:

# Find active Java installation path:
sudo update-alternatives --list java
# Output:
# /usr/lib/jvm/java-17-openjdk-amd64/bin/java
# /usr/lib/jvm/java-21-openjdk-amd64/bin/java

# Add JAVA_HOME to /etc/environment:
echo 'JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64"' | sudo tee -a /etc/environment

# Load the updated environment in current session:
source /etc/environment

# Verify JAVA_HOME variable:
echo $JAVA_HOME
# Output: /usr/lib/jvm/java-21-openjdk-amd64

Step 5: Testing with a Sample Hello World Java Program

Verify your Java installation and compiler by writing, compiling, and running a simple Java class:

Create a file named HelloWorld.java in your terminal:

Compile the source code into bytecode using javac, then execute it with java:

# Create Hello World Java file:
cat << 'EOF' > HelloWorld.java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Java is running successfully on Ubuntu 24.04 LTS!");
        System.out.println("Java Version: " + System.getProperty("java.version"));
    }
}
EOF

# Compile Java class:
javac HelloWorld.java

# Execute compiled class:
java HelloWorld
# Output:
# Java is running successfully on Ubuntu 24.04 LTS!
# Java Version: 21.0.4

Troubleshooting and Common Errors

When configuring Ubuntu servers, unexpected permission errors, network timeouts, or lock contention can occur. Review these common issues and verified resolutions:

javac: command not found

Cause: Only the JRE (runtime) was installed instead of the JDK (development kit).

Resolution: Install the full JDK package using sudo apt install -y openjdk-21-jdk.

sudo apt install -y openjdk-21-jdk

JAVA_HOME not set error in Maven or Gradle

Cause: The JAVA_HOME environment variable is not defined or points to a non-existent directory.

Resolution: Define JAVA_HOME in /etc/environment pointing to /usr/lib/jvm/java-21-openjdk-amd64 and run source /etc/environment.

echo $JAVA_HOME

UnsupportedClassVersionError: has been compiled by a more recent version of the Java Runtime

Cause: Bytecode was compiled with a newer Java version (e.g. Java 21) but executed on an older runtime (e.g. Java 17).

Resolution: Switch the active Java runtime to match the compiled bytecode using sudo update-alternatives --config java.

sudo update-alternatives --config java

Security Best Practices and Hardening

Keep Java updated with automated security patches by regularly executing sudo apt update && sudo apt upgrade.

For headless cloud production servers, use the -headless package variants (e.g. openjdk-21-jdk-headless) to avoid pulling unnecessary X11 desktop GUI dependencies.

Never run production Java application processes as the root user; create dedicated system user accounts (e.g. useradd -r -s /bin/false tomcat).

Deepen your Linux system administration skills with our related production-tested guides:

Authoritative References and Official Documentation

Frequently Asked Questions

Quick answers to common questions and configurations

4 FAQs
OpenJDK is the completely open-source, reference implementation of Java licensed under GPLv2 with Classpath Exception. Oracle JDK is built on the same OpenJDK codebase but carries proprietary commercial licensing terms for enterprise production use. OpenJDK is recommended for all Ubuntu systems.
Yes. You can download official Oracle JDK Debian packages (.deb) from Oracle's website and install them using sudo dpkg -i jdk-21_linux-x64_bin.deb, but OpenJDK is the community standard.
OpenJDK 21 is an LTS release supported with active security maintenance and bug updates throughout the entire 5-year standard lifecycle of Ubuntu 24.04 LTS (until 2029), and up to 10 years with Ubuntu Pro.
Headless packages (like openjdk-21-jre-headless) omit graphics, audio, and keyboard input libraries (such as AWT and Swing), resulting in a substantially smaller installation footprint ideal for cloud servers and Docker images.
RR
Written byVerified Author

Rajesh Ray

I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.

SPAM-FREE MODERATED DISCOURSE

Join the Conversation & Perspective

Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.

AT
MP
JV
+2

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Cloud & DevOps•

How to Close File Descriptors Above 9 in Dash Shell

Attempting to close a file descriptor above 9 in Dash using exec 10<&- results in a 10: not found error because Dash's POSIX lexer only recognizes single-digit file descriptors (0–9) in redirection operators. This guide explains the exact parser limitation, why FDs 10+ are reserved for internal shell state, and how to safely close inherited file descriptors using close_range, Python, or Perl wrappers.

RR
Rajesh Rai
Read More
Featured Infrastructure PartnerUp to 75% OFF + 3 Months Free

Supercharge Your Linux & Cloud Workloads with Hostinger

Deploy high-speed NVMe KVM VPS, Docker containers, and LiteSpeed-accelerated WordPress servers in under 60 seconds with full root access, dedicated IPv4, and 24/7 technical support.

Pure NVMe SSD StorageFull Root Access & SSHFree Domain & Unlimited SSL99.9% Uptime SLA
Plans from$2.99/mo
Claim Hostinger DealAffiliate link • Special discount applied at checkout