How to set a specific Java version for Maven
Codemia Knowledge Hub ·
Maven builds with the JDK that the JAVA_HOME environment variable points to, or the java on your PATH if JAVA_HOME is not set. So to change Maven's Java version, point JAVA_HOME at the JDK you want. Inside a project, set the language level with maven-compiler-plugin, and use Maven toolchains when different projects need different JDKs without touching JAVA_HOME.
Set JAVA_HOME
Windows:
- Right-click
My Computerand selectProperties. - Open
Advanced system settingsand clickEnvironment Variables. - Under System Variables, click
Newand add:- Name:
JAVA_HOME - Value: the JDK path (e.g.,
C:\Program Files\Java\jdk1.8.0_261)
Linux/Mac:
- Open Terminal.
- Add this line to your shell configuration file (
.bashrc,.bash_profile,.zshrc, etc.):
- Reload it with
source ~/.bashrcor open a new terminal session.
Set source and target in maven-compiler-plugin
For a project-level setting, configure maven-compiler-plugin in pom.xml:
source sets the Java language level the code is compiled as, and target sets the bytecode version of the compiled classes. These do not switch the JDK: the compilation still runs on the JDK Maven itself runs on (from JAVA_HOME), so that JDK must be able to produce the requested level.
Use toolchains for multiple JDKs
Maven toolchains let a project pick a JDK without changing JAVA_HOME each time. Define the JDKs in toolchains.xml, usually in the Maven .m2 directory:
Then reference it from pom.xml with the compiler plugin's jdkToolchain parameter (maven-compiler-plugin 3.6 or later):
Which approach to use
| Method | What it does | When to use it |
JAVA_HOME environment variable | Points Maven at the desired JDK, system-wide or per session. | A single project, or several projects that need the same Java version. |
maven-compiler-plugin in pom.xml | Sets the source and target Java versions in the POM. | Pinning the project's language level and bytecode version, independent of each developer's setup. |
| Maven toolchains | Configures multiple JDKs in toolchains.xml and references them from the project. | Projects that must compile against different JDKs without changing environment variables. |
Related reading
- How to set tableName dynamically using environment variable in spring boot?
- How to set text color of a TextView programmatically
- How to set the environment variables for Java in Windows
- How to set the java.library.path from Eclipse
- How to show full-file Git blame in Visual Studio Code
- How to show Git log history i.e., all the related commits for a sub directory of a Git repository
- How to set the maximum memory usage for JVM?
- How to set thousands separator in Java?

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.