How to Install OpenJDK 17 on Ubuntu

OpenJDK 17 is a free, open-source implementation of the Java Platform used to run and develop Java applications. As a Long-Term Support (LTS) release with community support extending through September 2029, it provides the stability and modern language features that developers and enterprises require. By the end of this guide, you will have OpenJDK 17 installed on Ubuntu using the default APT repository, with the ability to manage multiple Java versions and verify your installation.

Compare OpenJDK LTS Releases for Ubuntu

Before installing, confirm that OpenJDK 17 matches your project requirements. The table below compares LTS Java versions available in Ubuntu’s default repositories.

Java VersionLTS Support UntilChoose It WhenTrade-offs
OpenJDK 8December 2030 (community)Legacy applications, older frameworks like Spring 4.x, or vendor-certified deployments requiring Java 8Missing modern language features (no var, no records, no virtual threads); some libraries dropping Java 8 support
OpenJDK 11September 2027 (community)Enterprise applications, build servers, projects needing stable LTS with module system support and modern TLSLacks Java 21 features; some newer frameworks require 17+ as minimum
OpenJDK 17September 2029 (community)Spring Boot 3.x, Jakarta EE 10+, or workloads benefiting from records and sealed classesMissing virtual threads and newer pattern matching features from Java 21
OpenJDK 21September 2031 (community)New projects, high-concurrency applications using virtual threads, or development requiring the latest stable LTS featuresNewest LTS release; verify all dependencies support Java 21 before migrating production workloads

Choose OpenJDK 17 when starting new projects, when your frameworks require Java 17 as a minimum (Spring Boot 3.x, Jakarta EE 10+), or when you want access to modern language features like records, sealed classes, and pattern matching. For legacy applications with Java 8 or 11 requirements, consider the corresponding OpenJDK 8 or OpenJDK 11 guides instead.

Update Ubuntu Before OpenJDK 17 Installation

Start by updating your Ubuntu system to ensure all packages are current and avoid potential conflicts during installation:

sudo apt update
sudo apt upgrade

The apt update command refreshes your package index, while apt upgrade installs the latest versions of all installed packages. Depending on how many packages require updates, this process may take a few minutes.

Install OpenJDK 17 on Ubuntu

Ubuntu’s default repositories include OpenJDK 17 packages, making installation straightforward without adding external sources. Before installing, you can view the available OpenJDK 17 packages:

apt-cache search openjdk-17

This command displays all packages related to OpenJDK 17. You should see output similar to:

openjdk-17-jdk - OpenJDK Development Kit (JDK)
openjdk-17-jdk-headless - OpenJDK Development Kit (JDK) (headless)
openjdk-17-jre - OpenJDK Java runtime, using Hotspot JIT
openjdk-17-jre-headless - OpenJDK Java runtime, using Hotspot JIT (headless)
openjdk-17-source - OpenJDK Development Kit (JDK) source files

Install JRE or JDK

Next, choose the package that matches your needs. If you only need to run Java applications, install the Java Runtime Environment (JRE):

sudo apt install openjdk-17-jre

Alternatively, if you plan to develop Java applications or use build tools like Apache Maven, install the full Java Development Kit (JDK). This includes the JRE plus development tools such as the Java compiler:

sudo apt install openjdk-17-jdk

The JRE is sufficient for running Java applications, while the JDK is required for development purposes. If unsure, install the JDK since it includes everything in the JRE.

Verify the Installation

After installation completes, verify that Java is accessible by checking the installed version:

java --version

You should see output confirming OpenJDK 17 is installed:

openjdk 17.0.x 20xx-xx-xx
OpenJDK Runtime Environment (build 17.0.x+x-Ubuntu-x)
OpenJDK 64-Bit Server VM (build 17.0.x+x-Ubuntu-x, mixed mode, sharing)

If you installed the JDK, also verify the Java compiler is available:

javac --version
javac 17.0.x

Manage Multiple Java Versions

If you have multiple Java versions installed on your system, Ubuntu provides the update-alternatives command to switch between them. This is particularly useful when different projects require different Java versions.

Switch the Default Java Runtime

To view all installed Java versions and select one as the system default, run:

sudo update-alternatives --config java

This command displays an interactive menu showing all available Java installations with their paths and priorities:

There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                         Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-17-openjdk-amd64/bin/java   1711      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1111      manual mode
  2            /usr/lib/jvm/java-17-openjdk-amd64/bin/java   1711      manual mode

Press <enter> to keep the current choice[*], or type selection number:

Enter the selection number corresponding to your desired Java version and press Enter. After making your selection, verify the change with java --version.

Switch the Default Java Compiler

If you installed multiple JDK versions, you should also switch the Java compiler to match the runtime. Otherwise, you may compile with one version but run with another, causing compatibility issues:

sudo update-alternatives --config javac

Select the same version number you chose for the runtime to keep your development environment consistent.

Set the JAVA_HOME Environment Variable

Many Java applications and build tools require the JAVA_HOME environment variable to locate your Java installation. For detailed instructions on configuring this variable persistently, see our guide on setting the Java environment path in Ubuntu.

As a quick reference, you can find the path to your active Java installation with:

dirname $(dirname $(readlink -f $(which java)))

On 64-bit Ubuntu systems with OpenJDK 17 as the active version, this returns:

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

To set this as your JAVA_HOME for the current user, add the following to your ~/.bashrc file:

echo 'export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64' >> ~/.bashrc
source ~/.bashrc

Verify the variable is set correctly:

echo $JAVA_HOME
/usr/lib/jvm/java-17-openjdk-amd64

Test the Installation

To confirm that Java compiles and runs correctly, create a simple test program. This step verifies that both the runtime and the compiler are functioning as expected.

Create a Test Program

Create a new Java source file using your preferred text editor:

nano Hello.java

Next, add the following Java code to the file:

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello from OpenJDK 17!");
        System.out.println("Java version: " + System.getProperty("java.version"));
    }
}

Save the file by pressing Ctrl+O, then exit with Ctrl+X.

Compile and Run

Now, compile the Java source code into bytecode:

javac Hello.java

If compilation succeeds without errors, a Hello.class file appears in the current directory. Run the compiled program with:

java Hello

Expected output:

Hello from OpenJDK 17!
Java version: 17.0.x

This output confirms your OpenJDK 17 installation is working correctly.

Troubleshoot Common Issues

This section covers common problems you may encounter when working with OpenJDK 17 on Ubuntu.

Wrong Java Version Running

If java --version shows a different version than expected, you likely have multiple Java installations. Check which versions are available:

sudo update-alternatives --config java

Select the correct version number and verify the change took effect. If you also develop Java applications, remember to switch the compiler as well:

sudo update-alternatives --config javac

JAVA_HOME Not Set or Incorrect

Build tools like Maven and Gradle require JAVA_HOME to function. If you receive errors about JAVA_HOME being undefined, first find your Java installation path:

dirname $(dirname $(readlink -f $(which java)))

Then add the result to your shell configuration. For bash users:

echo 'export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64' >> ~/.bashrc
source ~/.bashrc

Confirm the variable is set:

echo $JAVA_HOME

Compilation Fails with Version Errors

If compilation fails with messages about unsupported class file versions, your compiler and runtime versions don’t match. Ensure both use the same Java version:

java --version
javac --version

Both commands should report the same major version number. If they differ, use update-alternatives to align them.

Remove OpenJDK 17

If you no longer need OpenJDK 17, remove it along with any automatically installed dependencies:

sudo apt remove openjdk-17-jdk openjdk-17-jre
sudo apt autoremove

The apt remove command removes the main OpenJDK 17 packages, while apt autoremove cleans up any orphaned dependencies that were installed alongside Java but are no longer needed.

After removal, verify that Java is no longer available or has switched to another installed version:

java --version

If no other Java versions are installed, this command returns an error confirming removal. If another version remains, the output shows which Java is now active.

If you previously set JAVA_HOME in your ~/.bashrc file, either remove that line or update it to point to a different Java version. Otherwise, applications that rely on JAVA_HOME will fail with “directory not found” errors.

Conclusion

You now have OpenJDK 17 installed on Ubuntu and configured as your Java runtime. With the update-alternatives system, you can easily switch between multiple Java versions as needed for different projects. For Java development workflows, consider installing Apache Maven as a build tool. If your projects require a different LTS release, explore OpenJDK 8 for legacy applications or OpenJDK 11 for enterprise environments.

Useful Resources

For additional information about Java releases and downloads, these official resources may help:

Leave a Comment