How to Install Java 21 and Set Java Environment Variables System-Wide on Ubuntu

How to Install Java 21 and Set Java Environment Variables System-Wide on Ubuntu

Installing Java 21 on Ubuntu is a common requirement for Java-based applications such as DSpace, Tomcat, Maven, and other enterprise applications.

This guide covers installing OpenJDK 21, configuring JAVA_HOME and JAVA_OPTS system-wide, and verifying the installation.

In this guide:
  • Install OpenJDK 21
  • Verify Java and Javac
  • Find the Java installation path
  • Configure JAVA_HOME system-wide
  • Configure JAVA_OPTS
  • Verify the complete Java environment
  • Important considerations for Tomcat

Step 1: Update the System Packages

Before installing Java, update the Ubuntu package repository information.

sudo apt update

Purpose: Update the package repository information.

Explanation: Downloads the latest package information from the configured Ubuntu repositories so that the system can locate the latest available OpenJDK 21 packages.

Step 2: Install OpenJDK 21

Install Java 21 using the Ubuntu package manager.

sudo apt install openjdk-21-jdk -y

Purpose: Install OpenJDK 21 JDK.

Explanation: Installs the Java Development Kit version 21, including the Java runtime, compiler, and other development tools required by Java applications and build systems such as Maven.

Step 3: Verify the Java Installation

After installation, verify that Java 21 is installed correctly.

java -version

Purpose: Verify the installed Java version.

Explanation: Displays the currently active Java runtime version. The output should show Java 21.

Example output:

openjdk version "21.x.x"
OpenJDK Runtime Environment
OpenJDK 64-Bit Server VM

Step 4: Verify the Java Compiler

Check that the Java compiler is also available.

javac -version

Purpose: Verify the Java compiler.

Explanation: Confirms that the JDK, rather than only the Java Runtime Environment, has been installed correctly.

Expected output:

javac 21.x.x

Step 5: Find the Java Installation Path

Before configuring JAVA_HOME, determine the exact Java installation path.

update-alternatives --list java

Purpose: Find the exact Java installation path.

Explanation: Lists all Java versions registered with Ubuntu along with their full installation paths. This is particularly useful when multiple Java versions are installed on the same server.

For Java 21, the path will normally look similar to:

/usr/lib/jvm/java-21-openjdk-amd64/bin/java
Note: The exact directory name can vary depending on the Ubuntu version, architecture, and Java package installed. Always verify the actual path on your server.

Step 6: Check the Active Java Version

If multiple Java versions are installed, check which version Ubuntu is currently using.

update-alternatives --config java

Purpose: Select the default Java version.

Explanation: Displays all Java versions registered on the system and allows you to select Java 21 as the default version.

Select the entry corresponding to Java 21.

After selecting it, verify again:

java -version

Step 7: Edit /etc/environment

Configure Java environment variables system-wide.

sudo nano /etc/environment

Purpose: Edit the system-wide environment configuration.

Explanation: Opens /etc/environment, which is used to define environment variables that should be available system-wide rather than only to one user's shell.

Step 8: Configure JAVA_HOME

Add the following line to /etc/environment:

JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64"

Purpose: Set JAVA_HOME for Java 21.

Explanation: Tells Java-based applications, build tools, Maven, Ant, and application servers exactly where the Java 21 JDK is installed.

Step 9: Configure JAVA_OPTS

Add the following line below JAVA_HOME:

JAVA_OPTS="-Xmx512M -Xms64M -Dfile.encoding=UTF-8"

Purpose: Configure default Java runtime options.

Explanation: Sets the maximum Java heap size to 512 MB, the initial heap size to 64 MB, and forces UTF-8 file encoding.

The values can be increased depending on the application's memory requirements.

Step 10: Save the File

If you are using nano:

Ctrl+O
Enter
Ctrl+X

This saves /etc/environment and exits the editor.

Correct:

JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64"

Incorrect:

export JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64"
Important: Do not use export inside /etc/environment. Variables in this file should be defined using the NAME="value" format.

Step 11: Apply the Environment Changes

Reload /etc/environment in the current terminal session.

source /etc/environment

Purpose: Apply the new environment variables.

Explanation: Loads the values from /etc/environment into the current shell without requiring you to log out and log back in.

Step 12: Verify JAVA_HOME

Check that JAVA_HOME is correctly configured.

echo $JAVA_HOME

Expected output:

/usr/lib/jvm/java-21-openjdk-amd64

Step 13: Verify JAVA_OPTS

Check the configured Java options.

echo $JAVA_OPTS

Expected output:

-Xmx512M -Xms64M -Dfile.encoding=UTF-8

Step 14: Verify Java Using JAVA_HOME

Run Java directly using the configured JAVA_HOME.

$JAVA_HOME/bin/java -version

Purpose: Confirm that JAVA_HOME points to a valid Java 21 installation.

Explanation: Executes the Java binary directly from the directory specified by JAVA_HOME. This provides an additional check that the configured path is correct.

Step 15: Verify the Complete Java Environment

Run the following commands together:

java -version
javac -version
echo $JAVA_HOME
echo $JAVA_OPTS

Purpose: Perform a final Java 21 environment check.

Explanation: Confirms the Java runtime, Java compiler, JAVA_HOME, and JAVA_OPTS configuration in one place.

Environment Variables Summary

Variable Used By Purpose
JAVA_HOME Maven, Ant, Tomcat, Java applications Tells applications exactly which JDK installation to use.
JAVA_OPTS Tomcat, standalone Java applications, some build scripts Provides JVM runtime options such as heap size and encoding.

Important Note for Tomcat

If Tomcat is being used for a DSpace installation, configuring JAVA_OPTS in /etc/environment may not be sufficient for Tomcat's own service process.

Tomcat may use its own environment or service configuration file, depending on how it was installed.

For example, an Ubuntu package installation of Tomcat may use:

/etc/default/tomcat10

Therefore, Java memory settings intended specifically for the Tomcat service should be checked and configured in the appropriate Tomcat service configuration.

DSpace / Tomcat Tip:

For production deployments, do not assume that JAVA_OPTS defined in /etc/environment will automatically be inherited by every systemd service. Always verify the environment of the actual service running Java.

Where People Usually Go Wrong

  1. Using the wrong JAVA_HOME path
    Always verify the actual JDK installation directory instead of assuming the path.
  2. ```
  3. Adding export to /etc/environment
    Use JAVA_HOME="...", not export JAVA_HOME="...".
  4. Installing only the JRE
    Development tools such as Maven require the JDK. Install openjdk-21-jdk.
  5. Having multiple Java versions installed
    Use update-alternatives --config java to verify which Java version is active.
  6. Assuming JAVA_OPTS applies to every application
    Application servers such as Tomcat may have their own service-specific configuration.
  7. Not starting a new session
    If environment variables are not available after modifying /etc/environment, log out and log back in, or reload the environment appropriately.
  8. ```

Final Verification Checklist

Run the following commands:

java -version
javac -version
echo $JAVA_HOME
echo $JAVA_OPTS
$JAVA_HOME/bin/java -version

Installation Complete

If all commands return the expected Java 21 information, Java 21 and the configured environment variables have been successfully set up.


Note: Commands and paths may vary slightly depending on the Ubuntu version, system architecture, installed Java versions, and the method used to install Java.

Comments