Maven
JDK
Java
Build Tool
Software Development

Specify which JDK Maven uses: JAVA_HOME and toolchains

Codemia Knowledge Hub ·

Maven uses the JDK that the JAVA_HOME environment variable points to, so setting JAVA_HOME is the simplest way to choose it. For a per-project choice, configure the maven-toolchains-plugin in pom.xml. Pinning the JDK keeps builds consistent across machines and collaborators, since different Java versions can introduce breaking changes or new features.

Setting JAVA_HOME

  1. Find the JDK installation directory. On a Unix-based system it might be /usr/lib/jvm/java-11-openjdk-amd64.
  2. Set the environment variable.
    For Linux/macOS:
bash
   export JAVA_HOME=/path/to/jdk
   export PATH=$JAVA_HOME/bin:$PATH

For Windows:

bash
   setx JAVA_HOME "C:\Path\To\JDK"
  1. Verify JAVA_HOME.
    In the terminal run:
bash
   echo $JAVA_HOME

On Windows, use:

bash
   echo %JAVA_HOME%

The Linux/macOS export applies to the current session, while setx on Windows sets it permanently.

Configuring the maven-toolchains-plugin

For projects that need a specific JDK version, the maven-toolchains-plugin lets you declare the JDK at the project or global level. Add this to pom.xml:

xml
1<build>
2    <plugins>
3        <plugin>
4            <groupId>org.apache.maven.plugins</groupId>
5            <artifactId>maven-toolchains-plugin</artifactId>
6            <version>3.1.0</version>
7            <executions>
8                <execution>
9                    <goals>
10                        <goal>toolchain</goal>
11                    </goals>
12                </execution>
13            </executions>
14            <configuration>
15                <toolchains>
16                    <jdk>
17                        <version>11</version>
18                        <vendor>oracle</vendor>
19                    </jdk>
20                </toolchains>
21            </configuration>
22        </plugin>
23    </plugins>
24</build>

This tells toolchain-aware plugins such as the compiler and Surefire to use a JDK 11 from Oracle. Maven itself still runs on the JDK in JAVA_HOME. The plugin looks the JDK up in ~/.m2/toolchains.xml, so declare it there:

xml
1<toolchains>
2    <toolchain>
3        <type>jdk</type>
4        <provides>
5            <version>11</version>
6            <vendor>oracle</vendor>
7        </provides>
8        <configuration>
9            <jdkHome>/path/to/jdk-11</jdkHome>
10        </configuration>
11    </toolchain>
12</toolchains>

What .mvn/jvm.config does (and does not do)

Some guides suggest a project-local .mvn directory for this, with a jvm.config file:

  1. Create the .mvn directory in the project root, if it does not exist.
  2. Create or edit jvm.config and add a line like:
 
   -Djdk.version=11
  1. Make sure the specified JDK is installed and accessible.

Be aware that .mvn/jvm.config only passes JVM options (heap size, system properties and so on) to the JVM that runs Maven. It does not select which JDK Maven runs on, and a -Djdk.version=11 property has no effect on that choice by itself. Use JAVA_HOME or toolchains to pick the JDK.

Why the JDK version matters

A specific JDK version lets you use newer Java features or stay compatible with legacy code. Problems often appear when:

  • Libraries depend on different Java versions.
  • New Java syntax isn't supported by older JDKs.

Check which JDK Maven picked

Run:

bash
mvn -version

The output shows the JDK and Java version Maven uses, along with other environment details.

MethodEnvironment Variable RequiredPer-project SetupGlobal/Local StrategyBest Use Case
Setting JAVA_HOMEYesNoGlobalQuickly switch JDK versions for all projects.
maven-toolchains-pluginNoYesLocal/GlobalProjects requiring specific JDK versions.
.mvn/jvm.configNoYesLocalPer-project JVM options for Maven (does not select the JDK).

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms