Why Maven uses JDK 1.6 but my java -version is 1.7

142,013

Solution 1

add the following to your ~/.mavenrc:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/{jdk-version}/Contents/Home

Second Solution:

echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile

Solution 2

Get into

/System/Library/Frameworks/JavaVM.framework/Versions

and update the CurrentJDK symbolic link to point to

/Library/Java/JavaVirtualMachines/YOUR_JDK_VERSION/Contents/

E.g.

cd /System/Library/Frameworks/JavaVM.framework/Versions
sudo rm CurrentJDK
sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/ CurrentJDK

Now it shall work immediately.

Solution 3

You can also do,

<properties>
      ...  

      <!-- maven-compiler-plugin , aka JAVA Compiler 1.7 -->
      <maven.compiler.target>1.7</maven.compiler.target>
      <maven.compiler.source>1.7</maven.compiler.source>

      ...  
</properties>

Solution 4

You can also explicitly tell maven which java version to compile for. You can try adding the maven-compiler-plugin to your pom.

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

If you imported a maven project into an IDE, then there is probably a maven setting in your IDE for default compiler that your maven runner is using.

Solution 5

It helped me. Just add it in your pom.xml.

By default maven compiler plugin uses Java 1.5 or 1.6, you have to redefine it in your pom.xml.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>
Share:
142,013

Related videos on Youtube

Ninja
Author by

Ninja

I am an early bird and a night owl.

Updated on July 08, 2022

Comments

  • Ninja
    Ninja almost 2 years

    I'm new to maven, and also to MacOS.

    I have setup maven in my terminal, and when getting the version settings (using mvn -v) it seems it uses JDK 1.6, while I have JDK 1.7 installed. Is there anything wrong?

    The commands I enter are these:

    blues:helloworld Ninja$ java -version
    
    java version "1.7.0_05"
    Java(TM) SE Runtime Environment (build 1.7.0_05-b06)
    Java HotSpot(TM) 64-Bit Server VM (build 23.1-b03, mixed mode)`
    
    blues:helloworld Ninja$ mvn -v
    
    Apache Maven 3.1.0 (893ca28a1da9d5f51ac03827af98bb730128f9f2; 2013-06-28 10:15:32+0800)
    Maven home: /usr/local/Cellar/maven/3.1.0/libexec
    Java version: 1.6.0_51, vendor: Apple Inc.
    Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
    Default locale: zh_CN, platform encoding: EUC_CN
    OS name: "mac os x", version: "10.8.4", arch: "x86_64", family: "mac"
    
    • Jeanne Boyarsky
      Jeanne Boyarsky almost 11 years
      Can you post your PATH and JAVA_HOME variables?
    • Ninja
      Ninja almost 11 years
      @JeanneBoyarsky blues:helloworld Ninja$ echo $JAVA_HOME blues:helloworld Ninja$ echo $PATH /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/‌​bin
    • Ninja
      Ninja almost 11 years
      @JeanneBoyarsky is this you want,sir?
    • Jeanne Boyarsky
      Jeanne Boyarsky almost 11 years
      I'm female (so referring to me as "sir" is incorrect) and yes. Please try setting your JAVA_HOME. Your problem seems similar to this issue: stackoverflow.com/questions/13752519/…
    • Ninja
      Ninja almost 11 years
      @JeanneBoyarsky Forgive my rudeness.The url you given is the problem I meet.Thanks a lot~
  • Ninja
    Ninja almost 11 years
    o,thank you. but I haven't start . I just 'mvn -v'...the reason is I don't assign JAVA_HOME
  • Ninja
    Ninja over 10 years
    yes,you answer is correct .The JAVAHOME is required.I edit the /etc/profile instead
  • gvaish
    gvaish over 10 years
    Well, adding to ~/.mavenrc ensures that other apps are not affected. Only mvn picks up this version of JDK. :)
  • jherranzm
    jherranzm about 10 years
    Thank you! Finally maven in Eclipse compiles again...!
  • Admin
    Admin about 10 years
    Note that only affects your code that is compiled. I ran into this problem and came here because an external library I was using was compiled for Java 7 and Maven was trying to run it with Java 6
  • jla
    jla about 10 years
    export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
  • Steve Cohen
    Steve Cohen almost 10 years
    wow. this is good. I just realized that the /etc/alternatives mess doesn't help me here. There is no alternative set up for the whole JDK. thanks. Saved me from asking a repeat question.
  • Gavin S. Yancey
    Gavin S. Yancey over 9 years
    That doesn't work if maven is running using java 6. It just crashes with [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project {projectname}: Fatal error compiling: invalid target release: 1.7 -> [Help 1]
  • Gavin S. Yancey
    Gavin S. Yancey over 9 years
    That doesn't work if maven is running using java 6. It just crashes with [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project {projectname}: Fatal error compiling: invalid target release: 1.7 -> [Help 1]
  • Stefan L
    Stefan L over 9 years
    The CurrentJDK symlink trick doesn't seem to work any more with maven 3.3.1, but modifying ~/.mavenrc does.
  • timothyclifford
    timothyclifford almost 9 years
    .mavenrc solution worked perfectly for me. Also realised I only had JDK 1.8 installed and not JRE 1.8. Thanks Gaurav!
  • krex
    krex almost 9 years
    Excellent. Other recommendations suggested setting /etc/mavenrc similarly. Just as a warning to anyone else: ~/.mavenrc appears to take precedence.
  • Paŭlo Ebermann
    Paŭlo Ebermann almost 8 years
    This seems to work without export too (at least in Ubuntu).
  • John Doe
    John Doe over 7 years
    This will work in case maven setting in your IDE for default compiler is lower than 1.7, but the installed jdk is higher than 1.7
  • Eddy
    Eddy almost 7 years
    If you are using jenv on a mac, it can be: export JAVA_HOME="$HOME/.jenv/versions/`jenv version-name`" for ~/.mavenrc file.
  • user2035039
    user2035039 over 4 years
    When using IntelliJ, it is also possible to set the JRE to be used by Maven via Settings > Build, Execution, Deployment > Build Tools > Maven > Runner