Switch Java Versions per Project: update-alternatives & JAVA_HOME
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…
Java 21 and Java 25 have been available as long-term support releases since September 2023 and September 2025 respectively. Many developers now find themselves with both versions installed on a single machine, often when working on different projects requiring distinct Java versions. However, managing system-wide defaults and per-project setups can be cumbersome, as switching the system default each time you switch between repositories can become tedious, and discrepancies between `java -version` and your build tool remain unresolved.
This tutorial presents a three-tiered approach to managing multiple Java versions: system default, shell or directory override, and per-build pinning. The tutorial then details a step-by-step checklist to troubleshoot the common issue where Java 25 is installed, but Maven continues to compile with Java 21.
First, install both Java 21 and Java 25 side by side using Ubuntu's package archive. On Ubuntu 24.04, `openjdk-21-jdk` is the default JDK, while `openjdk-25-jdk` is available in the Universe repository. Enable Universe and update the package index before installing the JDK packages. The JDK packages include both the runtime and development tools, so a separate JRE is unnecessary if you only need to run existing Java applications.
Next, understand the three levels that determine the Java version used by your system:
1. System-wide default using `update-alternatives`: This method uses Debian's alternatives system to manage multiple Java installations. The `/usr/bin/java` and `/usr/bin/javac` executables are symbolic links to different JDK installations, controlled by `update-alternatives`. You can set the system default by choosing an entry with `update-alternatives --config java` and `update-alternatives --config javac`. However, this method may not be ideal for developers switching between projects frequently.
2. Per-shell or per-directory override using `JAVA_HOME`: Setting the `JAVA_HOME` environment variable to the desired JDK's `bin` directory allows the JDK to be used by newly launched tools. You can create a shell function, `use-java`, to quickly switch between Java versions, and use `direnv` to automatically apply the correct Java version within a directory. This method is more flexible for developers who work on different projects requiring different Java versions.
3. Per-build pinning using Maven toolchains or Gradle toolchains and the daemon JVM: This method involves specifying the JDK version to use for each build, regardless of the shell or directory settings. Maven's `toolchains` feature or Gradle's `toolchains` plugin allows you to pin the JDK version used for compilation, ensuring consistency across builds.
When diagnosing a version mismatch, follow this order:
1. Check the system-wide default by running `java -version` and `javac -version`.
2. Verify the `JAVA_HOME` environment variable and the corresponding `PATH` configuration.
3. Finally, inspect the build tool's toolchain configuration to ensure the correct JDK version is being used.
With this structured approach, developers can effectively manage multiple Java versions, ensuring compatibility across their projects without the hassle of constantly switching system defaults.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to