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
- Find the JDK installation directory. On a Unix-based system it might be
/usr/lib/jvm/java-11-openjdk-amd64. - Set the environment variable.For Linux/macOS:
For Windows:
- Verify JAVA_HOME.In the terminal run:
On Windows, use:
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:
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:
What .mvn/jvm.config does (and does not do)
Some guides suggest a project-local .mvn directory for this, with a jvm.config file:
- Create the .mvn directory in the project root, if it does not exist.
- Create or edit
jvm.configand add a line like:
- 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:
The output shows the JDK and Java version Maven uses, along with other environment details.
| Method | Environment Variable Required | Per-project Setup | Global/Local Strategy | Best Use Case |
| Setting JAVA_HOME | Yes | No | Global | Quickly switch JDK versions for all projects. |
| maven-toolchains-plugin | No | Yes | Local/Global | Projects requiring specific JDK versions. |
.mvn/jvm.config | No | Yes | Local | Per-project JVM options for Maven (does not select the JDK). |
Related reading
- Specifying Java version in maven - differences between properties and compiler plugin
- Specifying trust store information in spring boot application.properties
- Speed up Spring Boot startup time
- SpEL used in Document indexName with spring data elasticsearch and spring boot is not being parsed
- Split Java String by New Line
- split string only on first instance - java
- Splitting a Java String by the pipe symbol using split
- Spontaneous NullPointerExceptions when firing Events
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.