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β¦