How to set specific Java version to Maven?

JavaMavenJava 7Java 6Maven Toolchains

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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.

  • You can set the JAVA_HOME parameter just before you start maven (and change it back afterwards if need be).
  • You could also go into your mvn(non-windows)/mvn.bat/mvn.cmd(windows) and set your java version explicitly there.

Solution 2 - Java

Edit:

A better solution is presented by the answer from Ondrej, which obviates remembering aliases.


Original Answer:

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.

Solution 3 - Java

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>

Solution 4 - Java

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.

Solution 5 - Java

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

Solution 6 - Java

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

Solution 7 - Java

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.

Solution 8 - Java

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.

Solution 9 - Java

On Macs, (assuming you have the right version installed)

JAVA_HOME=`/usr/libexec/java_home -v 1.8` mvn clean install -DskipTests

Solution 10 - Java

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.

Solution 11 - Java

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>

Solution 12 - Java

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>

Solution 13 - Java

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

Solution 14 - Java

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>

Solution 15 - Java

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" "$@"

Solution 16 - Java

On Linux/Unix, the JAVA_HOME within 'mvn' shell script is overridden by the settings in

$HOME/.mavenrc

please https://stackoverflow.com/questions/37905648/where-to-add-java-home-and-maven-path-variables-in-linux

Solution 17 - Java

I did not have success on mac with just setting JAVA_HOME in the console but I was successful with this approach

  • Create .mavenrc file at your at your home directory (so the file will path will be ~/.mavenrc
  • Into that file paste export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)

Solution 18 - Java

  1. on terminal vi ~/.bash_profile
  2. After that, paste the below path in the base profile file and save it by vim command 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
  1. run source ~/.bash_profile to make it work forever

  2. 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.

Solution 19 - Java

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.

Solution 20 - Java

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

Solution 21 - Java

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

Solution 22 - Java

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://stackoverflow.com/a/56487374/1582473

Solution 23 - Java

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 :)

Solution 24 - Java

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"  "$@"\

Solution 25 - Java

Ondrej's answer worked perfectly. It truly solves the problem. Few other things to do though as detailed in the toolchain documentation

  1. Add the maven-toolchains-plugin in your project POM
  2. Configure the maven-toolchains-plugin to use a specific JDK in the toolchain as configured in your toolchains.xml file.

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionandPatView Question on Stackoverflow
Solution 1 - JavaDanielBarbarianView Answer on Stackoverflow
Solution 2 - JavaJonathan E. LandrumView Answer on Stackoverflow
Solution 3 - JavaEdward RossView Answer on Stackoverflow
Solution 4 - JavaOndrej BurkertView Answer on Stackoverflow
Solution 5 - JavaRonan QuillevereView Answer on Stackoverflow
Solution 6 - JavaJonathanView Answer on Stackoverflow
Solution 7 - JavaRanukaView Answer on Stackoverflow
Solution 8 - JavaPankajView Answer on Stackoverflow
Solution 9 - Javauser2475448View Answer on Stackoverflow
Solution 10 - JavaGuo FerrellView Answer on Stackoverflow
Solution 11 - JavaRuslanView Answer on Stackoverflow
Solution 12 - JavaSaveendra EkanayakeView Answer on Stackoverflow
Solution 13 - JavaTechnotronicView Answer on Stackoverflow
Solution 14 - JavaToninoView Answer on Stackoverflow
Solution 15 - Javanew coderView Answer on Stackoverflow
Solution 16 - JavaBernhard ThalmayrView Answer on Stackoverflow
Solution 17 - JavaMartin VichView Answer on Stackoverflow
Solution 18 - JavaJINSHUAIView Answer on Stackoverflow
Solution 19 - Javaakimran82View Answer on Stackoverflow
Solution 20 - JavaAlberto PerezView Answer on Stackoverflow
Solution 21 - JavaGtdDevView Answer on Stackoverflow
Solution 22 - JavaVijay YadavView Answer on Stackoverflow
Solution 23 - JavaMohamed AliView Answer on Stackoverflow
Solution 24 - JavaJaesung ShinView Answer on Stackoverflow
Solution 25 - JavamikaeloviView Answer on Stackoverflow