<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nora Whitfield</title>
    <description>The latest articles on DEV Community by Nora Whitfield (@nora-whitfield).</description>
    <link>https://dev.to/nora-whitfield</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4271499%2F97061145-1ec8-4366-849b-3bcd62526f13.png</url>
      <title>DEV Community: Nora Whitfield</title>
      <link>https://dev.to/nora-whitfield</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nora-whitfield"/>
    <language>en</language>
    <item>
      <title>Switch Java Versions per Project: update-alternatives &amp; JAVA_HOME</title>
      <dc:creator>Nora Whitfield</dc:creator>
      <pubDate>Mon, 30 Nov 2026 04:46:58 +0000</pubDate>
      <link>https://dev.to/nora-whitfield/switch-java-versions-per-project-update-alternatives-javahome-1c2f</link>
      <guid>https://dev.to/nora-whitfield/switch-java-versions-per-project-update-alternatives-javahome-1c2f</guid>
      <description>&lt;p&gt;The &lt;a href="https://openjdk.org/projects/jdk/21/" rel="noopener noreferrer"&gt;JDK 21 release page&lt;/a&gt; lists general availability on 19 September 2023, and &lt;a href="https://openjdk.org/projects/jdk/25" rel="noopener noreferrer"&gt;JDK 25&lt;/a&gt; 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 &lt;code&gt;java -version&lt;/code&gt; and your build tool disagree.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Install Java 21 and Java 25 Side by Side&lt;/li&gt;
&lt;li&gt;The Three Levels That Decide a Java Version&lt;/li&gt;
&lt;li&gt;Level 1: Set the System-Wide Default with update-alternatives&lt;/li&gt;
&lt;li&gt;Level 2: Pin per Shell or per Directory with JAVA_HOME&lt;/li&gt;
&lt;li&gt;
Level 3: Pin the Compiler per Build

&lt;ul&gt;
&lt;li&gt;Maven toolchains&lt;/li&gt;
&lt;li&gt;Gradle toolchains&lt;/li&gt;
&lt;li&gt;Gradle daemon JVM&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Diagnose a Version Mismatch in the Right Order&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Install Java 21 and Java 25 Side by Side
&lt;/h2&gt;

&lt;p&gt;Both LTS releases install cleanly from Ubuntu's package archive. On Ubuntu 24.04, &lt;code&gt;openjdk-21-jdk&lt;/code&gt; is the version behind &lt;code&gt;default-jdk&lt;/code&gt;, while &lt;code&gt;openjdk-25-jdk&lt;/code&gt; comes from the Universe repository and is published in &lt;code&gt;noble-updates&lt;/code&gt;. If the 25 package is not found, enable Universe and refresh the index first, as the &lt;a href="https://linuxize.com/post/install-java-on-ubuntu-24-04" rel="noopener noreferrer"&gt;Linuxize guide to installing Java on Ubuntu 24.04&lt;/a&gt; explains.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo add-apt-repository universe
sudo apt update
sudo apt install openjdk-21-jdk openjdk-25-jdk
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Release&lt;/th&gt;
&lt;th&gt;Package&lt;/th&gt;
&lt;th&gt;Typical &lt;code&gt;JAVA_HOME&lt;/code&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Java 21&lt;/td&gt;
&lt;td&gt;&lt;code&gt;openjdk-21-jdk&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;/usr/lib/jvm/java-21-openjdk-amd64&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Java 25&lt;/td&gt;
&lt;td&gt;&lt;code&gt;openjdk-25-jdk&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;/usr/lib/jvm/java-25-openjdk-amd64&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;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 &lt;a href="https://javaspring.net/blog/java-runtime-environment-latest-download/" rel="noopener noreferrer"&gt;guide to downloading the latest JRE for Java 21 and Java 25&lt;/a&gt; covers what the runtime includes and how the LTS releases differ. With both JDKs staged, you can verify them directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;/usr/lib/jvm/java-21-openjdk-amd64/bin/java -version
/usr/lib/jvm/java-25-openjdk-amd64/bin/java -version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The Three Levels That Decide a Java Version
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Level&lt;/th&gt;
&lt;th&gt;Configured with&lt;/th&gt;
&lt;th&gt;What it controls&lt;/th&gt;
&lt;th&gt;Scope&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;System default&lt;/td&gt;
&lt;td&gt;&lt;code&gt;update-alternatives&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Which &lt;code&gt;/usr/bin/java&lt;/code&gt; and &lt;code&gt;/usr/bin/javac&lt;/code&gt; symlinks point to&lt;/td&gt;
&lt;td&gt;Whole machine&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Shell or directory&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;JAVA_HOME&lt;/code&gt; and &lt;code&gt;PATH&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Which JDK a newly started tool sees&lt;/td&gt;
&lt;td&gt;One terminal or one directory&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Build&lt;/td&gt;
&lt;td&gt;Maven toolchains or Gradle toolchains and daemon JVM&lt;/td&gt;
&lt;td&gt;Which JDK compiles and runs the project, regardless of the launching shell&lt;/td&gt;
&lt;td&gt;One repository&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Level 1: Set the System-Wide Default with update-alternatives
&lt;/h2&gt;

&lt;p&gt;Debian's alternatives system keeps a generic name such as &lt;code&gt;/usr/bin/java&lt;/code&gt; pointing at one of several interchangeable implementations. The link is not direct: &lt;code&gt;/usr/bin/java&lt;/code&gt; points into &lt;code&gt;/etc/alternatives&lt;/code&gt;, which points at the selected JDK, so an administrator can change the choice without touching &lt;code&gt;/usr/bin&lt;/code&gt;. The &lt;a href="https://manpages.debian.org/trixie/dpkg/update-alternatives.1.en.html" rel="noopener noreferrer"&gt;update-alternatives manual page&lt;/a&gt; documents the full model, including the difference between automatic and manual mode.&lt;/p&gt;

&lt;p&gt;List what is registered, then choose interactively:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo update-alternatives --list java
sudo update-alternatives --config java
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pick the Java 25 entry and repeat for the compiler, which is a separate link group:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo update-alternatives --config javac
java -version
javac -version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;That is why scripts should use the non-interactive form rather than feeding numbers to a menu:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;JAVA25&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/usr/lib/jvm/java-25-openjdk-amd64
sudo update-alternatives --set java &lt;span class="s2"&gt;"$JAVA25/bin/java"&lt;/span&gt;
sudo update-alternatives --set javac &lt;span class="s2"&gt;"$JAVA25/bin/javac"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;To undo a manual choice and let the system pick the highest-priority candidate again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo update-alternatives --auto java
sudo update-alternatives --auto javac
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Level 2: Pin per Shell or per Directory with JAVA_HOME
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;JAVA_HOME&lt;/code&gt; is the conventional pointer to a JDK installation. Prepending its &lt;code&gt;bin&lt;/code&gt; directory to &lt;code&gt;PATH&lt;/code&gt; also makes &lt;code&gt;java&lt;/code&gt;, &lt;code&gt;javac&lt;/code&gt; and friends resolve to that JDK ahead of the alternatives symlinks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;JAVA_HOME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/usr/lib/jvm/java-21-openjdk-amd64
&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"$JAVA_HOME/bin:$PATH"&lt;/span&gt;
java -version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;A small shell function saves typing and prevents path typos:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;use-java&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;local&lt;/span&gt; &lt;span class="nv"&gt;version&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"${1:?usage: use-java 21|25}"&lt;/span&gt;
  &lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;JAVA_HOME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"/usr/lib/jvm/java-${version}-openjdk-amd64"&lt;/span&gt;
  &lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"$JAVA_HOME/bin:$PATH"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Put that in &lt;code&gt;~/.bashrc&lt;/code&gt; or &lt;code&gt;~/.zshrc&lt;/code&gt;, then run &lt;code&gt;use-java 21&lt;/code&gt; or &lt;code&gt;use-java 25&lt;/code&gt;.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;JAVA_HOME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/usr/lib/jvm/java-21-openjdk-amd64
PATH_add &lt;span class="s2"&gt;"$JAVA_HOME/bin"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The limitation is that a shell can only have one &lt;code&gt;JAVA_HOME&lt;/code&gt; 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Level 3: Pin the Compiler per Build
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;
  
  
  Maven toolchains
&lt;/h3&gt;

&lt;p&gt;Maven toolchains let a project declare the JDK used for compilation separately from the JDK that runs Maven itself. Two pieces are required: the &lt;code&gt;maven-toolchains-plugin&lt;/code&gt; in the POM, and a &lt;code&gt;toolchains.xml&lt;/code&gt; file on the build machine. Maven's &lt;a href="https://maven.apache.org/guides/mini/guide-using-toolchains.html" rel="noopener noreferrer"&gt;guide to using toolchains&lt;/a&gt; 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.&lt;/p&gt;

&lt;p&gt;Create &lt;code&gt;~/.m2/toolchains.xml&lt;/code&gt; with an entry per installed JDK:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;toolchains&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;toolchain&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;type&amp;gt;&lt;/span&gt;jdk&lt;span class="nt"&gt;&amp;lt;/type&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;provides&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;21&lt;span class="nt"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/provides&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;jdkHome&amp;gt;&lt;/span&gt;/usr/lib/jvm/java-21-openjdk-amd64&lt;span class="nt"&gt;&amp;lt;/jdkHome&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/toolchain&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;toolchain&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;type&amp;gt;&lt;/span&gt;jdk&lt;span class="nt"&gt;&amp;lt;/type&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;provides&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;25&lt;span class="nt"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/provides&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;jdkHome&amp;gt;&lt;/span&gt;/usr/lib/jvm/java-25-openjdk-amd64&lt;span class="nt"&gt;&amp;lt;/jdkHome&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/toolchain&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/toolchains&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then request a version in the project's &lt;code&gt;pom.xml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;plugin&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.apache.maven.plugins&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;maven-toolchains-plugin&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;3.3.0&lt;span class="nt"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;executions&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;execution&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;goals&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;goal&amp;gt;&lt;/span&gt;toolchain&lt;span class="nt"&gt;&amp;lt;/goal&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/goals&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/execution&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/executions&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;toolchains&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;jdk&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;21&lt;span class="nt"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/jdk&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/toolchains&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/plugin&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Remember that &lt;code&gt;mvn -v&lt;/code&gt; 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.&lt;/p&gt;

&lt;h3&gt;
  
  
  Gradle toolchains
&lt;/h3&gt;

&lt;p&gt;Gradle's equivalent is a toolchain block in the build script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;java&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;toolchain&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;languageVersion&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;JavaLanguageVersion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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 &lt;code&gt;JAVA_HOME&lt;/code&gt;, SDKMAN, asdf and Maven's &lt;code&gt;toolchains.xml&lt;/code&gt;, so the JDKs you installed in step one are usually found without extra setup. The &lt;a href="https://docs.gradle.org/current/userguide/toolchains.html" rel="noopener noreferrer"&gt;Gradle toolchain documentation&lt;/a&gt; lists the precedence rules; notably, the JVM currently running Gradle is preferred over other installations, and JDKs are preferred over JREs.&lt;/p&gt;

&lt;p&gt;Run the built-in report to see exactly what the build can use and how each installation was discovered:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./gradlew -q javaToolchains
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Gradle daemon JVM
&lt;/h3&gt;

&lt;p&gt;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 &lt;a href="https://docs.gradle.org/current/userguide/gradle_daemon.html" rel="noopener noreferrer"&gt;Gradle daemon documentation&lt;/a&gt; distinguishes the client JVM that starts the wrapper from the long-lived daemon, and notes that daemon JVM criteria take precedence over &lt;code&gt;JAVA_HOME&lt;/code&gt; and &lt;code&gt;org.gradle.java.home&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Declare the daemon's requirements with a generated properties file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./gradlew updateDaemonJvm --jvm-version=25
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This writes &lt;code&gt;gradle/gradle-daemon-jvm.properties&lt;/code&gt; with a &lt;code&gt;toolchainVersion&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;org.gradle.java.home&lt;/code&gt; in a project &lt;code&gt;gradle.properties&lt;/code&gt; 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 &lt;code&gt;org.gradle.java.home&lt;/code&gt; for a personal &lt;code&gt;~/.gradle/gradle.properties&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Diagnose a Version Mismatch in the Right Order
&lt;/h2&gt;

&lt;p&gt;When &lt;code&gt;java -version&lt;/code&gt; 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.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Check the shell first.&lt;/strong&gt; Run &lt;code&gt;java -version&lt;/code&gt;, &lt;code&gt;javac -version&lt;/code&gt;, &lt;code&gt;readlink -f "$(which java)"&lt;/code&gt; and &lt;code&gt;echo "$JAVA_HOME"&lt;/code&gt;. If the resolved path is not the JDK you expected, an earlier &lt;code&gt;PATH&lt;/code&gt; entry — often a hardcoded &lt;code&gt;export PATH=.../jdk...&lt;/code&gt; in a profile file — is overriding the alternatives link.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check how Maven was launched.&lt;/strong&gt; &lt;code&gt;mvn -v&lt;/code&gt; prints Maven's own "Java version" and the runtime path, as shown in the &lt;a href="https://maven.apache.org/install.html" rel="noopener noreferrer"&gt;Maven installation guide&lt;/a&gt;. If that line says 21, Maven is inheriting a 21 &lt;code&gt;JAVA_HOME&lt;/code&gt; or an older &lt;code&gt;java&lt;/code&gt; on &lt;code&gt;PATH&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check the compile toolchain.&lt;/strong&gt; If &lt;code&gt;mvn -v&lt;/code&gt; shows 25 but compilation fails or targets 21, inspect &lt;code&gt;maven-toolchains-plugin&lt;/code&gt; in the effective POM and confirm that &lt;code&gt;~/.m2/toolchains.xml&lt;/code&gt; has a matching version string. This is the layer that &lt;code&gt;mvn -v&lt;/code&gt; cannot show you.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check Gradle's two JVMs.&lt;/strong&gt; &lt;code&gt;./gradlew --version&lt;/code&gt; 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.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;List the toolchains.&lt;/strong&gt; &lt;code&gt;./gradlew -q javaToolchains&lt;/code&gt; 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.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Restart the daemon.&lt;/strong&gt; The daemon caches project configuration, including toolchain paths and versions, so changes may not take effect until it is replaced. Run &lt;code&gt;./gradlew --stop&lt;/code&gt; and rebuild.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Read the daemon log.&lt;/strong&gt; If the build still picks the wrong JVM, open &lt;code&gt;~/.gradle/daemon/&amp;lt;gradle-version&amp;gt;/daemon-&amp;lt;pid&amp;gt;.out.log&lt;/code&gt; and look for the Java home the daemon started with. The PID matches the entry in &lt;code&gt;./gradlew --status&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check the IDE last.&lt;/strong&gt; 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.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Use &lt;code&gt;update-alternatives&lt;/code&gt; for the machine-wide default, &lt;code&gt;JAVA_HOME&lt;/code&gt; for a shell or a directory, and toolchains for anything that must be reproducible. Maven pins the compiler through &lt;code&gt;toolchains.xml&lt;/code&gt; plus &lt;code&gt;maven-toolchains-plugin&lt;/code&gt;; Gradle pins compilation through the &lt;code&gt;java&lt;/code&gt; toolchain block and pins the build process itself through &lt;code&gt;gradle-daemon-jvm.properties&lt;/code&gt;. When the versions disagree, check &lt;code&gt;java -version&lt;/code&gt;, then &lt;code&gt;mvn -v&lt;/code&gt;, 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.&lt;/p&gt;

</description>
      <category>java</category>
      <category>linux</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
