On my machine I have two Java versions installed: (1.6 and 1.7 installed manually by me). I need both of them for different projects. But for Maven I need 1.7, but my Maven uses the 1.6 Java version.
How can I set Maven to use 1.7?
On my machine I have two Java versions installed: (1.6 and 1.7 installed manually by me). I need both of them for different projects. But for Maven I need 1.7, but my Maven uses the 1.6 Java version.
How can I set Maven to use 1.7?
Maven uses the JAVA_HOME
parameter to find which Java version it is supposed to run. I see from your comment that you can't change that in the configuration.
JAVA_HOME
parameter just before you start maven (and change it back afterwards if need be). mvn
(non-windows)/mvn.bat
/mvn.cmd
(windows) and set your java version explicitly there.JAVA_HOME
in your local shell without changing the configuration of your machine at all (as soon as you close that shell, the change is gone). You can even change it for a single command: JAVA_HOME=/path/to/jdk17/ mvn build
–
Bigot A better solution is presented by the answer from Ondrej, which obviates remembering aliases.
Adding a solution for people with multiple Java versions installed
We have a large codebase, most of which is in Java. The majority of what I work on is written in either Java 1.7 or 1.8. Since JAVA_HOME
is static, I created aliases in my .bashrc
for running Maven with different values:
alias mvn5="JAVA_HOME=/usr/local/java5 && mvn"
alias mvn6="JAVA_HOME=/usr/local/java6 && mvn"
alias mvn7="JAVA_HOME=/usr/local/java7 && mvn"
alias mvn8="JAVA_HOME=/usr/local/java8 && mvn"
This lets me run Maven from the command line on my development machine regardless of the JDK version used on the project.
mvn2
and mvn3
defined and the naming gets a bit funky when Java versions also come into play... :D –
Salvation alias mvn8 = "JAVA_HOME=`/usr/libexec/java_home -v 1.8` && mvn
–
Afterdeck &&
before mvn
. mvn does not see JAVA_HOME's value with &&
. –
Cupellation &&
that just worked fine for me now on Ubuntu 20 and default bash –
Coarsen In the POM, you can set the compiler properties, e.g. for 1.8:
<project>
...
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
...
</project>
package javax.xml.bind.annotation does not exist
error when I try to compile a Java 8 project, even after adding the properties you mentioned. If I set JAVA_HOME
to the java8 folder, mvn
works fine again. –
Authenticity I just recently, after seven long years with Maven, learned about toolchains.xml. Maven has it even documented and supports it from 2.0.9 - toolchains documentation
So I added a toolchains.xml file to my ~/.m2/ folder with following content:
<toolchains xmlns="http://maven.apache.org/TOOLCHAINS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/TOOLCHAINS/1.1.0 http://maven.apache.org/xsd/toolchains-1.1.0.xsd">
<!-- JDK toolchains -->
<toolchain>
<type>jdk</type>
<provides>
<version>1.8</version>
<vendor>sun</vendor>
</provides>
<configuration>
<jdkHome>/opt/java8</jdkHome>
</configuration>
</toolchain>
<toolchain>
<type>jdk</type>
<provides>
<version>1.7</version>
<vendor>sun</vendor>
</provides>
<configuration>
<jdkHome>/opt/java7</jdkHome>
</configuration>
</toolchain>
</toolchains>
It allows you to define what different JDKs Maven can use to build the project irrespective of the JDK Maven runs with. Sort of like when you define JDK on project level in IDE.
/opt/java8
? –
Cyclohexane mvn clean install
, but when I unzip the resulting JAR and check the file MANIFEST.MF
I always see the JDK defined by the system PATH
and JAVA_HOME
. –
Kristofor On windows
If you do not want to change your JAVA_HOME
variable inside the system variables.
Edit your mvn.bat
file and add a line like this
set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_45\jre
This can be done after @REM ==== START VALIDATION ====
like mentionned by @Jonathan
On Mac (& Linux ?)
If you do not want to change your JAVA_HOME
variable inside your ~/.bashrc
or ~/.bash_profile
you can create a ~/.mavenrc
file and redefine your JAVA_HOME
using the java_home tool
export JAVA_HOME=`/usr/libexec/java_home -v 1.7.0_45`
Sanity Check
You can verify that everything is working fine by executing the following commands. The jdk version should be different.
mvn -version
then
java -version
Adding my two cents and explicitly providing the solution.
I have two JDKs installed on my Windows Machine - JDK 1.5
and JDK 1.6
.
My default (and set to windows system environment variable) JAVA_HOME
is set to JDK 1.5
.
However, I have a maven project that I need to build (i.e., JBehave Tutorial's Etsy.com) using JDK 1.6
.
My solution in this scenario (which worked!), is as suggested by @DanielBarbarian
to set it in mvn.bat
.
For some not familiar with window's batch file, I just basically added the set JAVA_HOME=<path_to_other_jdk>
line after @REM ==== START VALIDATION ====
in mvn.bat
(i.e., %MAVEN_HOME%\bin\mvn.bat
):
@REM ==== START VALIDATION ====
set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_45\jre
if not "%JAVA_HOME%" == "" goto OkJHome
mvn.bat
is renamed to mvn.cmd
. –
Lablab You can set Maven to use any java version following the instructions below.
Install jenv in your machine link
Check the available java versions installed in your machine by issuing the following command in command line.
jenv versions
You can specify global Java version using the following command.
jenv global oracle64-1.6.0.39
You can specify local Java version for any directory(Project) using the following command in the directory in command line.
jenv local oracle64-1.7.0.11
add the correct java version in your pom.xml
if you are running maven in command line install jenv maven plugin using below command
jenv enable-plugin maven
Now you can configure any java version in your machine to any project with out any trouble.
One simple solution to the problem -
JAVA_HOME=/usr/lib/jvm/java-6-sun/ mvn clean install
On Mac, it would look something like -
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_21.jdk/Contents/Home/ mvn clean install
PS: One special case that i found is the above given command does not work on 'fish' shell. I also had bash shell available and it worked fine there. just use command 'bash' to switch to bash shell.
You could configure compiling sources using different JDK with maven-compiler-plugin
.
Just specify path to javac
in <executable>
tag. E.g for java11 it looks like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>11</source>
<target>11</target>
<fork>true</fork>
<executable>C:\Program Files\Java\jdk-11.0.1\bin\javac</executable> <!--PATH TO JAVAC -->
</configuration>
</plugin>
On Macs, (assuming you have the right version installed)
JAVA_HOME=`/usr/libexec/java_home -v 1.8` mvn clean install -DskipTests
I am using Mac and none of the answers above helped me. I found out that maven loads its own JAVA_HOME from the path specified in: ~/.mavenrc
I changed the content of the file to be:
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home
For Linux it will look something like:
export JAVA_HOME=/usr/lib/jvm/java-8-oracle/jre
On windows, I just add multiple batch files for different JDK versions to the Maven bin folder like this:
mvn11.cmd
@echo off
setlocal
set "JAVA_HOME=path\to\jdk11"
set "path=%JAVA_HOME%;%path%"
mvn %*
then you can use mvn11
to run Maven in the specified JDK.
Without changing Environment Variables, You can manage java version based on the project level by using Maven Compiler Plugin.
Method 1
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Method 2
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
To avoid any impact to your project and to your Environment Variables, you can configure the Maven Compiler Plugin just to the project's POM, specifying the Source and Target java version
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
...
</plugins>
Recently installed Java10 to see whats new and fun about it. Once I did this and tried running existing projects which use java8.
To my surprise, maven began using java10 as its default java version, even though my JAVA_HOME is set to use java8 — /usr/libexec/java_home -v 1.8
Given, my installation was done using brew — Brew is simply a package manager for Mac OS, my M2_HOME was automatically set up. This is usually in /usr/local/Cellar/maven/3.5.4
With this new found knowledge, I run
nano /usr/local/Cellar/maven/3.5.4/bin/mvn
The content was
#!/bin/bash
JAVA_HOME="${JAVA_HOME:-$(/usr/libexec/java_home)}" exec "/usr/local/Cellar/maven/3.5.4/libexec/bin/mvn" "$@"
Important bit being
"${JAVA_HOME:-$(/usr/libexec/java_home)}"
To resolve this, you will need to specify the java version you need maven to default to. So in my case, I needed java8.
Update /usr/local/Cellar/maven/3.5.4/bin/mvn
file as follows
#!/bin/bash
JAVA_HOME="${JAVA_HOME:-$(/usr/libexec/java_home -v 1.8)}" exec "/usr/local/Cellar/maven/3.5.4/libexec/bin/mvn" "$@"
which java
. which java
points me to /usr/bin/java
which is /etc/alternatives/java
and will update when new version is installed. –
Beare I am using MAC Mini - M1 chip, after installing maven through brew install maven I noticed it's using java 19 but I rather wanted to use java 17 version that I downloaded in to my machine. Was out of luck with specific instructions to reset the default mvn java version. Here are the steps you can follow.
mvn --version
#this will give where the maven is installed
in my case Maven home: /opt/homebrew/Cellar/maven/3.8.7/libexec
/opt/homebrew/Cellar/maven/3.8.7/bin
foldervi mvn
#!/bin/bash
JAVA_HOME="${JAVA_HOME:-/Library/Java/JavaVirtualMachines/jdk-17.0.3.1.jdk/Contents/Home}" exec "/opt/homebrew/Cellar/maven/3.8.7/libexec/bin/mvn" "$@"
mvn -v
will now show it's default java version is 17Apache Maven 3.8.7 (b89d5959fcde851dcb1c8946a785a163f14e1e29)
Maven home: /opt/homebrew/Cellar/maven/3.8.7/libexec
Java version: 17.0.3.1, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-17.0.3.1.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "13.1", arch: "aarch64", family: "mac"
Ondrej's answer worked perfectly. It truly solves the problem. Few other things to do though as detailed in the toolchain documentation
See sample configurations below:
toolchains.xml
<toolchains xmlns="http://maven.apache.org/TOOLCHAINS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/TOOLCHAINS/1.1.0 http://maven.apache.org/xsd/toolchains-1.1.0.xsd">
<!-- JDK toolchains -->
<toolchain>
<type>jdk</type>
<provides>
<version>1.8</version>
<vendor>sun</vendor>
</provides>
<configuration>
<jdkHome>/Library/Java/JavaVirtualMachines/jdk1.8.0_301.jdk/Contents/Home</jdkHome>
</configuration>
</toolchain>
<toolchain>
<type>jdk</type>
<provides>
<version>17</version>
<vendor>sun</vendor>
</provides>
<configuration>
<jdkHome>/Library/Java/JavaVirtualMachines/jdk-17.0.2.jdk/Contents/Home</jdkHome>
</configuration>
</toolchain>
<toolchain>
<type>jdk</type>
<provides>
<version>11</version>
<vendor>sun</vendor>
</provides>
<configuration>
<jdkHome>/Library/Java/JavaVirtualMachines/jdk-11.0.12.jdk/Contents/Home</jdkHome>
</configuration>
</toolchain>
</toolchains>
As seen above, this works for machines having multiple java versions installed. Then in the project's POM, the maven-toolchains-plugin is configured to use one of the JDK versions defined as a toolchain.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
<configuration>
<toolchains>
<jdk>
<version>11</version>
<vendor>sun</vendor>
</jdk>
</toolchains>
</configuration>
</plugin>
I did not have success on mac with just setting JAVA_HOME
in the console but I was successful with this approach
~/.mavenrc
export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
vi ~/.bash_profile
wq!
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home
export M2_HOME=/Users/mj/Tools/apache-maven-3.8.1
run source ~/.bash_profile
to make it work forever
after that, you can run mvn -v
in your terminal to check
Maven home: /Users/mj/Tools/apache-maven-3.8.1
Java version: 1.8.0_211, vendor: Oracle Corporation,
runtime: /Users/mj/Tools/jdk1.8.0_211.jdk/Contents/Home/jre
Default locale: en_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.16", arch: "x86_64", family: "mac"
if you want to know why, read below
JAVA_HOME is used by many Java-based applications to define the place of Java Runtime Environment (JRE) installation. M2_HOME is used by Maven, and again it tells the program where to find Maven installation.
echo $JAVA_HOME
and it's empty), just as @new coder said. –
Beare You just need to add your JAVA version to mvn file before the call of JAVA_HOME for example my JAVA Version in .bashrc file is Java-17 and i want to run maven with Java-11 :
in file /usr/share/maven/bin/mvn
:
I add :
JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
before the call of JAVA_HOME
in the script
and then it works fine with Java-11 :)
On Linux/Unix, the JAVA_HOME within 'mvn' shell script is overridden by the settings in
$HOME/.mavenrc
please Where to add JAVA_HOME and MAVEN path variables in linux
set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_45\jre
Adding the above line of code as the first statement in $MAVEN_HOME\bin\mvn.cmd
worked for me straight away in Windows 10.
I couldn't solve this problem with the answers above and I tried many things to solve this and I got it. I hope someone like me could solve the problem.
I am using brew to install java and maven. For me, I wanted to use java 1.8. So I installed java 1.8 but still maven use version 17.0.2
Solution:
install java 1.8
I referred to this website to install it: https://devqa.io/brew-install-java/
mvn --version
and remember the version \
cat /usr/local/Cellar/maven/VERSION(for me 3.8.5)/bin/mvn
Then you will see
#!/bin/bash
JAVA_HOME="${JAVA_HOME:-/usr/local/opt/openjdk/libexec/openjdk.jdk/Contents/Home}" exec "/usr/local/Cellar/maven/3.8.5/libexec/bin/mvn" "$@"\
Check this directory
ls /usr/local/opt
You will see files like openjdk, openjdk@17, openjdk@8
change the directory openjdk to openjdk@8 in mvn file:
#!/bin/bash
JAVA_HOME="${JAVA_HOME:-/usr/local/opt/openjdk@8/libexec/openjdk.jdk/Contents/Home}" exec "/usr/local/Cellar/maven/3.8.5/libexec/bin/mvn" "$@"\
Mac Users, If you have installed maven from HomeBrew(Brew), Or even if you have installed it from elsewhere, If you want to change the default version of the Java used by the Maven, You can use following commands to change the version that the maven uses,
vim ~/.mavenrc
In the gui that pops up enter this and save-
JAVA_HOME="(put the Home path of your desired java version package)"
To verify wether the path is changed for maven you can check using -
mvn -v
Also you can have two versions of maven installed, and edit one of them, editing here:
mvn(non-windows)/mvn.bat/mvn.cmd(windows)
replacing your %java_home% appearances to your java desired path. Then just execute maven from that modified path
I've used the base idea from @Jonathan. I've set the windows with: set JAVA_HOME=C:\Program Files\java\AdoptOpenJDK-11.0.8+10 call mvn clean package -DskipTests
I am using jenv and was facing the compilation error for "javax.xml.bind.annotation" as the mvn in terminal was using openjdk 13
Maven home: /usr/local/Cellar/maven/3.6.3_1/libexec
Java version: 13.0.2, vendor: N/A, runtime: /usr/local/Cellar/openjdk/13.0.2+8_2/libexec/openjdk.jdk/Contents/Home
For me, it worked after running 'jenv enable-plugin maven' as suggested here https://mcmap.net/q/99007/-how-to-set-specific-java-version-to-maven
Setting the Default Java Version You might have 2 different versions of Java on your system. To set one as the default , use the command:
sudo alternatives ––config java
The system displays a list of different Java versions. If you like the default, press Enter.
If you want to change it, type the number of the version you want, then press Enter.
I used https://mcmap.net/q/99007/-how-to-set-specific-java-version-to-maven and created the below functions for use with Powershell:
function J08 { $env:JAVA_HOME = 'C:\Program Files\Java\jdk1.8.0_161'}
function J09 { $env:JAVA_HOME = 'C:\Program Files\Java\jdk-9'}
function J11 { $env:JAVA_HOME = 'C:\Program Files\Java\jdk-11.0.10'}
function J15 { $env:JAVA_HOME = 'C:\Program Files\Java\jdk-15.0.2'}
function J16 { $env:JAVA_HOME = 'C:\Program Files\Java\jdk-16.0.1'}
function J17 { $env:JAVA_HOME = 'C:\Program Files\Java\jdk-17'}
function J18 { $env:JAVA_HOME = 'C:\Program Files\Java\jdk-18.0.2'}
function J19 { $env:JAVA_HOME = 'C:\Program Files\Java\jdk-19'}
These functions must be placed in file C:\Program Files\PowerShell\7\profile.ps1
(or any other PowerShell profile) and be called before use. for example, if you want to use maven with Java 8, you must first call J08
, then call mvn ...
.
I found the toolchain answer so complicated and preferred this way.
© 2022 - 2025 — McMap. All rights reserved.