The JDK 21 release page lists general availability on 19 September 2023, and JDK 25 followed on 16 September 2025; both are long-term support releases from most vendors. Since then, many developers have ended up with both versions on one laptop: an older service that must stay on 21, and a newer project that wants 25. The trap is that changing the system default every time you switch repositories gets old quickly, and it still does not explain why java -version and your build tool disagree.

This tutorial sets up a three-level model — system default, shell or directory override, and per-build pinning — then gives you an ordered checklist for the classic "Java 25 is installed, but Maven keeps compiling with Java 21" problem.

Table of Contents

  • Install Java 21 and Java 25 Side by Side
  • The Three Levels That Decide a Java Version
  • Level 1: Set the System-Wide Default with update-alternatives
  • Level 2: Pin per Shell or per Directory with JAVA_HOME
  • Level 3: Pin the Compiler per Build
    • Maven toolchains
    • Gradle toolchains
    • Gradle daemon JVM
  • Diagnose a Version Mismatch in the Right Order
  • Conclusion

Install Java 21 and Java 25 Side by Side

Both LTS releases install cleanly from Ubuntu's package archive. On Ubuntu 24.04, openjdk-21-jdk is the version behind default-jdk, while openjdk-25-jdk comes from the Universe repository and is published in noble-updates. If the 25 package is not found, enable Universe and refresh the index first, as the Linuxize guide to installing Java on Ubuntu 24.04 explains.

sudo add-apt-repository universe sudo apt update sudo apt install openjdk-21-jdk openjdk-25-jdk 

Each package lands in its own directory under /usr/lib/jvm. The directory name ends in -amd64 on Intel and AMD machines and -arm64 on ARM systems, so confirm the real path before you copy it into a configuration file:

Release Package Typical JAVA_HOME
Java 21 openjdk-21-jdk /usr/lib/jvm/java-21-openjdk-amd64
Java 25 openjdk-25-jdk /usr/lib/jvm/java-25-openjdk-amd64

The JDK packages also include the runtime, so you do not need a separate JRE to run the applications you build. If you only need to execute Java programs rather than compile them, a JRE is sufficient; this guide to downloading the latest JRE for Java 21 and Java 25 covers what the runtime includes and how the LTS releases differ. With both JDKs staged, you can verify them directly:

/usr/lib/jvm/java-21-openjdk-amd64/bin/java -version /usr/lib/jvm/java-25-openjdk-amd64/bin/java -version 

The Three Levels That Decide a Java Version

The confusion almost always comes from mixing up three separate settings. They are independent, and the most specific one that applies to a given command wins.

Level Configured with What it controls Scope
System default update-alternatives Which /usr/bin/java and /usr/bin/javac symlinks point to Whole machine
Shell or directory JAVA_HOME and PATH Which JDK a newly started tool sees One terminal or one directory
Build Maven toolchains or Gradle toolchains and daemon JVM Which JDK compiles and runs the project, regardless of the launching shell One repository

Level 1: Set the System-Wide Default with update-alternatives

Debian's alternatives system keeps a generic name such as /usr/bin/java pointing at one of several interchangeable implementations. The link is not direct: /usr/bin/java points into /etc/alternatives, which points at the selected JDK, so an administrator can change the choice without touching /usr/bin. The update-alternatives manual page documents the full model, including the difference between automatic and manual mode.

List what is registered, then choose interactively:

sudo update-alternatives --list java sudo update-alternatives --config java 

Pick the Java 25 entry and repeat for the compiler, which is a separate link group:

sudo update-alternatives --config javac java -version javac -version 

In automatic mode the link group follows the highest-priority alternative, so installing a JDK with a higher priority can move the default without asking. Selecting a version by hand puts the group into manual mode, where your choice is kept until you change it or explicitly return to automatic mode.

That is why scripts should use the non-interactive form rather than feeding numbers to a menu:

JAVA25=/usr/lib/jvm/java-25-openjdk-amd64 sudo update-alternatives --set java "$JAVA25/bin/java" sudo update-alternatives --set javac "$JAVA25/bin/javac" 

Other tools such as jar, javadoc and keytool are registered in their own groups too. If you need them to follow the same JDK, loop over them or add the equivalent --set calls.

To undo a manual choice and let the system pick the highest-priority candidate again:

sudo update-alternatives --auto java sudo update-alternatives --auto javac 

This level is best when one JDK really is the machine-wide default and occasional switches are enough. It is a poor fit for a laptop that alternates between a Java 21 service and a Java 25 project all day.

Level 2: Pin per Shell or per Directory with JAVA_HOME

JAVA_HOME is the conventional pointer to a JDK installation. Prepending its bin directory to PATH also makes java, javac and friends resolve to that JDK ahead of the alternatives symlinks:

export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64 export PATH="$JAVA_HOME/bin:$PATH" java -version 

Close the terminal to drop the change, or unset the variable. Note that unsetting JAVA_HOME does not remove the directory you prepended to PATH, so the cleanest reset is a fresh shell.

A small shell function saves typing and prevents path typos:

use-java() { local version="${1:?usage: use-java 21|25}" export JAVA_HOME="/usr/lib/jvm/java-${version}-openjdk-amd64" export PATH="$JAVA_HOME/bin:$PATH" } 

Put that in ~/.bashrc or ~/.zshrc, then run use-java 21 or use-java 25.

For per-directory behaviour, an environment loader is more reliable than remembering to call the function. With direnv, create an .envrc in the repository root, run direnv allow, and direnv will export the values on entry and unload them when you leave. Its PATH_add helper prepends a directory to PATH safely:

export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64 PATH_add "$JAVA_HOME/bin" 

The limitation is that a shell can only have one JAVA_HOME at a time. If two builds run in parallel on the same agent, each needs its own process environment, which is exactly the case that the next level solves.

Level 3: Pin the Compiler per Build

Build tools can select a JDK independently of the one that launches them. That is the durable fix, because it travels with the repository instead of living in someone's dotfiles.

Maven toolchains

Maven toolchains let a project declare the JDK used for compilation separately from the JDK that runs Maven itself. Two pieces are required: the maven-toolchains-plugin in the POM, and a toolchains.xml file on the build machine. Maven's guide to using toolchains describes how the plugin stores the selected toolchain in the build session, where toolchain-aware plugins such as the compiler and Surefire pick it up.

Create ~/.m2/toolchains.xml with an entry per installed JDK:

<?xml version="1.0" encoding="UTF-8"?> <toolchains> <toolchain> <type>jdk</type> <provides> <version>21</version> </provides> <configuration> <jdkHome>/usr/lib/jvm/java-21-openjdk-amd64</jdkHome> </configuration> </toolchain> <toolchain> <type>jdk</type> <provides> <version>25</version> </provides> <configuration> <jdkHome>/usr/lib/jvm/java-25-openjdk-amd64</jdkHome> </configuration> </toolchain> </toolchains> 

Then request a version in the project's pom.xml:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-toolchains-plugin</artifactId> <version>3.3.0</version> <executions> <execution> <goals> <goal>toolchain</goal> </goals> </execution> </executions> <configuration> <toolchains> <jdk> <version>21</version> </jdk> </toolchains> </configuration> </plugin> 

Two details cause most of the grief. First, Maven compares the version strings literally: a toolchain that provides 21.0.9 does not satisfy a request for 21. Use a range such as [21,22) when the installed update version may vary. Second, the paths in toolchains.xml are machine-specific, so the file belongs in the user's home directory, not in version control. Since plugin version 3.2.0, toolchains:generate-jdk-toolchains-xml can discover the installed JDKs and write the entries for you.

Remember that mvn -v reports the JDK that launched Maven, not the toolchain JDK. A project can run Maven on 25 and compile with 21, and that is the intended behaviour.

Gradle toolchains

Gradle's equivalent is a toolchain block in the build script:

java { toolchain { languageVersion = JavaLanguageVersion.of(21) } } 

Gradle detects locally installed JDKs, chooses one that matches the requested language version, and can download one when no installation matches, provided a toolchain download repository is configured. Auto-detection also understands JAVA_HOME, SDKMAN, asdf and Maven's toolchains.xml, so the JDKs you installed in step one are usually found without extra setup. The Gradle toolchain documentation lists the precedence rules; notably, the JVM currently running Gradle is preferred over other installations, and JDKs are preferred over JREs.

Run the built-in report to see exactly what the build can use and how each installation was discovered:

./gradlew -q javaToolchains 

Gradle daemon JVM

Compiling with Java 21 is only half the story: the Gradle daemon that executes the build is itself a JVM, and it can differ from both the launcher JVM and the compile toolchain. The Gradle daemon documentation distinguishes the client JVM that starts the wrapper from the long-lived daemon, and notes that daemon JVM criteria take precedence over JAVA_HOME and org.gradle.java.home.

Declare the daemon's requirements with a generated properties file:

./gradlew updateDaemonJvm --jvm-version=25 

This writes gradle/gradle-daemon-jvm.properties with a toolchainVersion entry and per-platform download URLs. Commit the file: everyone who checks out the repository, including CI, will then start the daemon on a compatible JVM. Generating it requires a toolchain download repository, such as the Foojay resolver plugin, to be configured.

org.gradle.java.home in a project gradle.properties also selects the daemon JVM, but it takes an absolute path, so committing it breaks every machine that uses a different layout. Prefer the daemon criteria file for anything shared, and reserve org.gradle.java.home for a personal ~/.gradle/gradle.properties.

Diagnose a Version Mismatch in the Right Order

When java -version prints 25 but your build still behaves like Java 21, work from the outermost layer inward. Each step tells you which configuration is actually winning.

  1. Check the shell first. Run java -version, javac -version, readlink -f "$(which java)" and echo "$JAVA_HOME". If the resolved path is not the JDK you expected, an earlier PATH entry — often a hardcoded export PATH=.../jdk... in a profile file — is overriding the alternatives link.
  2. Check how Maven was launched. mvn -v prints Maven's own "Java version" and the runtime path, as shown in the Maven installation guide. If that line says 21, Maven is inheriting a 21 JAVA_HOME or an older java on PATH.
  3. Check the compile toolchain. If mvn -v shows 25 but compilation fails or targets 21, inspect maven-toolchains-plugin in the effective POM and confirm that ~/.m2/toolchains.xml has a matching version string. This is the layer that mvn -v cannot show you.
  4. Check Gradle's two JVMs. ./gradlew --version reports the launcher JVM and, on recent versions, the daemon JVM criteria. A mismatch between those two lines explains builds that start on 25 but execute on 21.
  5. List the toolchains. ./gradlew -q javaToolchains shows every detected JDK, its language version, whether it is a full JDK, and what detected it. A missing 25 entry here means toolchain auto-detection never saw the installation.
  6. Restart the daemon. The daemon caches project configuration, including toolchain paths and versions, so changes may not take effect until it is replaced. Run ./gradlew --stop and rebuild.
  7. Read the daemon log. If the build still picks the wrong JVM, open ~/.gradle/daemon/<gradle-version>/daemon-<pid>.out.log and look for the Java home the daemon started with. The PID matches the entry in ./gradlew --status.
  8. Check the IDE last. IntelliJ IDEA and Eclipse have their own Gradle JVM setting, which can override what you see in the terminal. Align it with the JDK you intend the build to use.

Conclusion

Use update-alternatives for the machine-wide default, JAVA_HOME for a shell or a directory, and toolchains for anything that must be reproducible. Maven pins the compiler through toolchains.xml plus maven-toolchains-plugin; Gradle pins compilation through the java toolchain block and pins the build process itself through gradle-daemon-jvm.properties. When the versions disagree, check java -version, then mvn -v, then the build's own toolchain configuration, and finish with the Gradle daemon and its logs. That order turns a confusing mismatch into a short list of settings you can actually inspect.