187

I am trying to build a Hudson plugin I've modified and it requires jdk1.6. This is fine, but I don't see how I can tell maven where the different jdk is. I've found few mentions on the internet but they don't seem to apply to me. Some suggest adding some config to .m2/settings.xml but I don't have a settings.xml. Plus, I don't want to use 1.6 for all maven builds.

One kink is I am using mvn in cygwin, if that matters at all. It appears I should be able to make the specification in the project pom file, but the existing pom is pretty bare.

So bottom line is, is there a way to specify a jdk for a single invocation of maven?

1

15 Answers 15

192

So bottom line is, is there a way to specify a jdk for a single invocation of maven?

Temporarily change the value of your JAVA_HOME environment variable.

10
  • 18
    Example in windows: set JAVA_HOME="C:\Java\jdk7"
    – acdcjunior
    Jun 3, 2014 at 17:28
  • 7
    in lubuntu: JAVA_HOME="/home/desa/programas/jdks/jdk1.6.0_45/" mvn -v Mar 12, 2015 at 1:41
  • 10
    And in case anyone else forgets and spends ages wondering why they can't change it: JAVA_HOME can be set for Maven (on Mac at least) in this file: /private/etc/mavenrc - And that can use something like (note the backticks not single quotes!): export JAVA_HOME=`/usr/libexec/java_home -v 1.7.0_75`
    – RedYeti
    Mar 27, 2015 at 13:23
  • 4
    Unix: export JAVA_HOME='D:/dev/java/jdk8/jre' (works for me) Mar 2, 2016 at 9:16
  • 4
    if I change JAVA_HOME and then I do java -version, it still prints the previous version.
    – ocramot
    Jan 18, 2019 at 14:45
105

Seems that maven now gives a solution here : Compiling Sources Using A Different JDK

Let's say your JAVA_HOME points to JDK7 (which will run maven processes)

Your pom.xml could be :

<build>
    <plugins>
        <!-- we want JDK 1.6 source and binary compatiblility -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <!-- ... -->
        <!-- we want sources to be processed by a specific 1.6 javac -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
              <verbose>true</verbose>
              <fork>true</fork>
              <executable>${JAVA_1_6_HOME}/bin/javac</executable>
              <compilerVersion>1.3</compilerVersion>
            </configuration>
        </plugin>
    </plugins>
</build>

If your developpers just add (and customize) the following lines in their settings.xml, your pom will be platform independant :

<settings>
  [...]
  <profiles>
    [...]
    <profile>
      <id>compiler</id>
        <properties>
          <JAVA_1_4_HOME>C:\Program Files\Java\j2sdk1.4.2_09</JAVA_1_4_HOME>
          <JAVA_1_6_HOME>C:\Program Files\Java\j2sdk1.6.0_18</JAVA_1_6_HOME>
        </properties>
    </profile>
  </profiles>
  [...]
  <activeProfiles>
    <activeProfile>compiler</activeProfile>
  </activeProfiles>
</settings>
5
  • 31
    Voted up! I found I can use -Dmaven.compiler.fork=true and -Dmaven.compiler.executable=/path/to/target/javac in command line.
    – Jin Kwon
    Jan 21, 2015 at 3:43
  • even using those java-opts, you still must add this to the compiler plugin <executable>${maven.compiler.executable}</executable> Feb 10, 2015 at 14:05
  • 2
    @JinKwon Passing options with -D works fine, even without defining it in the compiler plugin section. This is nice for occasional use or for scripting. You should put it in a separate answer so we can vote it up! Apr 15, 2016 at 8:19
  • This solution failed when I try to run tests in Java 8 I was getting *Unsupported major.minor version 52.0 *
    – edwin
    Jul 27, 2017 at 9:37
  • I was hoping to be able to use the same method to generate the JAR file using the maven-jar-plugin but it looks like it is not supported. The only method to generate JAR using a specific JDK is by setting PATH and JAVA_HOME. Any other method you are aware of?
    – tarekahf
    Dec 9, 2022 at 2:16
56

compile:compile has a user property that allows you to specify a path to the javac.

Note that this user property only works when fork is true which is false by default.

$ mvn -Dmaven.compiler.fork=true -Dmaven.compiler.executable=/path/to/the/javac compile

You might have to double quote the value if it contains spaces.

> mvn -Dmaven.compiler.fork=true -Dmaven.compiler.executable="C:\...\javac" compile

See also Maven custom properties precedence.

1
  • I've also needed to overwrite my JAVA_HOME variable. For example (in bash shell): JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/ mvn -Dmaven.compiler.fork=true -Dmaven.compiler.executable=/usr/lib/jvm/java-8-openjdk-amd64/bin/javac spring-boot:run Mar 31, 2020 at 19:02
23

As u said "Plus, I don't want to use 1.6 for all maven builds."....So better I will say modify your pom file and specify which jdk version to use.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>1.9</source>
                <target>1.9</target>
            </configuration>
        </plugin>
    </plugins>
</build>

It will ensure that your particular project uses that version of jdk.

2
  • 8
    This is just the starting point, but not the solution. This is the requirement for the maven compiler plugin to compile for 1.7. And then the trick is to make maven really capable of compiling for 1.7, which is not so trivial if your current java version is different... Jan 16, 2015 at 3:13
  • That is other question stackoverflow.com/questions/16723533/… Nov 11, 2016 at 17:40
18

I say you setup the JAVA_HOME environment variable like Pascal is saying: In Cygwin if you use bash as your shell should be:

export JAVA_HOME=/cygdrive/c/pathtothejdk

It never harms to also prepend the java bin directory path to the PATH environment variable with:

export PATH=${JAVA_HOME}/bin:${PATH}

Also add maven-enforce-plugin to make sure the right JDK is used. This is a good practice for your pom.

<build>
 <plugins>
   <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-enforcer-plugin</artifactId>
      <executions>
        <execution>
          <id>enforce-versions</id>
          <goals>
            <goal>enforce</goal>
          </goals>
          <configuration>
            <rules>
              <requireJavaVersion>
                <version>1.6</version>
              </requireJavaVersion>
            </rules>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Please, see Maven Enforcer plugin – Usage.

1
  • 1
    This was perfect for specifying the exact version of the JDK. For example, I have a project that fails when using the initial JDK 1.8, but if using JDK 1.8.0_77, it works just fine. I had both JDKs installed, and with this solution maven told me I was using the wrong version of 1.8 until I changed my JAVA_HOME path to target the specific 1.8.0_77 folder. The other answers did not allow you to be so granular on the version.
    – bojingo
    Mar 30, 2016 at 15:23
18

If you have installed Java through brew in Mac then chances are you will find your Java Home Directory here:

/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

The next step now would be to find which Java Home directory maven is pointing to. To find it type in the command:
mvn -version

enter image description here

The fields we are interested in here is: Java version and runtime.

Maven is currently pointing to Java 13. Also, you can see the Java Home path under the key runtime, which is:
/usr/local/Cellar/openjdk/13.0.2+8_2/libexec/openjdk.jdk/Contents/Home

To change the Java version of the maven, we need to add the Java 8 home path to the JAVA_HOME env variable.

To do that we need to run the command:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home in the terminal.

Now if we check the maven version, we can see that it is pointing to Java 8 now.

enter image description here

The problem with this is if you check the maven version again in the new terminal, you will find that it is pointing to the Java 13. To avoid this I would suggest adding the JAVA_HOME variable in the ~/.profile file.

This way whenever your terminal is loading it will take up the value you defined in the JAVA_HOME by default. This is the line you need to add in the ~/.profile file:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

You can open up a new terminal and check the Maven version, (mvn -version) and you will find it is pointing to the Java 8 this time.

2
  • I was checking by Bash profile and maven but both have different JAVA Versions. My JAVA_HOME looks: JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home My Maven reads the runtime as – /Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home Aug 19, 2022 at 11:04
  • One more thing. I tried to change JAVA_HOME, but despite doing so, the command (mvn -version) kept telling me that I had Java 13. What I did was set a wrong path in JAVA_HOME (with and error inside the path), and then the correct one. This caused the Java path to be nulled and then it was refreshed in Maven.
    – Lucas
    May 29 at 14:53
13

I know its an old thread. But I was having some issues with something similar to this in Maven for Java 8 compiler source. I figured this out with a quick fix mentioned in this article thought I can put it here and maybe can help others:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>
2
  • 1
    A Maven Update might be required after adding these lines (Eclipse: Right click on the project, Maven, Update project)
    – Romano
    Feb 11, 2019 at 15:59
  • 1
    Setting maven.compiler properties doesn't prevent the code from using methods added in java 11. For instances String.repeat(). This when building with jdk 11. So the maven builds but I wish it would fail Oct 24, 2019 at 5:47
7

Maven uses variable $JAVACMD as the final java command, set it to where the java executable is will switch maven to different JDK.

0
7

You could also set the JDK for Maven in a file in your home directory ~/.mavenrc:

JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk-11.0.5.jdk/Contents/Home'

This environment variable will be checked by the mvn script and used when present:

  if [ -f "$HOME/.mavenrc" ] ; then
    . "$HOME/.mavenrc"
  fi

https://github.com/CodeFX-org/mvn-java-9/tree/master/mavenrc

2
  • 1
    Highly underrated comment, with this I was able to persistently set the JAVA_HOME version in RHEL 8
    – barfoos
    Jun 16, 2021 at 8:54
  • 1
    Also worked perfectly on MacOS 12.4 with latest and version 11 of openjdk installed through homebrew Jun 20, 2022 at 14:23
3

Hudson also allows you to define several Java runtimes, and let you invoke Maven with one of these. Have a closer look on the configuration page.

1
  • The OP is building an hudson plugin on the command line, not under hudson (at least, this is my understanding). Mar 23, 2010 at 22:22
3

Yet another alternative to manage multiple jdk versions is jEnv

After installation, you can simply change java version "locally" i.e. for a specific project directory by:

jenv local 1.6

This will also make mvn use that version locally, when you enable the mvn plugin:

jenv enable-plugin maven
1
  • 1
    Thanks for the jenv maven recommendation.
    – RSharma
    Aug 10, 2020 at 18:47
3

I update my ~/.m2/settings.xml

<settings>
  <profiles>
        <profile>
            <id>j8</id>
            <profile>
            <id>j8</id>
            <properties>
                <maven.compiler.fork>true</maven.compiler.fork>
                <maven.compiler.executable>${env.JAVA_HOME8}/bin/javac.exe</maven.compiler.executable>
            </properties>
        </profile>
        <profile>
            <id>j11</id>
            <properties>
                <maven.compiler.fork>true</maven.compiler.fork>
                <maven.compiler.executable>${env.JAVA_HOME11}/bin/javac.exe</maven.compiler.executable>
            </properties>
        </profile>
  </profiles>
<settings>

For build with Java8 I run mvn with properties:

mvn compile -Pj8

and for Java 11

mvn compile -Pj11
2

If nothing else works and even after you set JAVA_HOME to a correct path, check if there is no override of the JAVA_HOME path in <user>/.mavenrc!

As a further tip, the mvn file is a bash script (on Linux).. so if necessary you can inspect the source [and change it].

0

I had build problem with maven within Eclipse on Windows 7.

Though I observed mvn build was running just fine from command line.

mvn -T 5 -B -e -X -U -P test clean install -Dmaven.surefire.debug  --settings ..\..\infra-scripts\maven-conf\settings.xml   > output.log

Eclipse was considering as default JVM a JRE installation instead of JDK so it was failing on compilation.

I added to eclipse.ini following line:

-vm
C:\Program Files (x86)\Java\jdk1.8.0_25\bin

Also when starting from eclipse I used in "Goals" section following list:

-T 5 -B -e -X -U -P test clean install -Dmaven.surefire.debug  --settings ..\..\infra-scripts\maven-conf\settings.xml

Compilation error got solved.

0

For Java 9 :

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>9</source>
                <target>9</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.