167

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"
starball
  • 20,030
  • 7
  • 43
  • 238
Ninja
  • 2,479
  • 3
  • 23
  • 32
  • 2
    Can you post your PATH and JAVA_HOME variables? – Jeanne Boyarsky Sep 15 '13 at 15:03
  • @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 Sep 15 '13 at 15:13
  • @JeanneBoyarsky is this you want,sir? – Ninja Sep 15 '13 at 15:14
  • 3
    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: http://stackoverflow.com/questions/13752519/how-to-change-maven-java-home – Jeanne Boyarsky Sep 15 '13 at 15:16
  • 2
    @JeanneBoyarsky Forgive my rudeness.The url you given is the problem I meet.Thanks a lot~ – Ninja Sep 15 '13 at 15:21

9 Answers9

239

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

A H K
  • 1,758
  • 17
  • 29
gvaish
  • 9,374
  • 3
  • 38
  • 43
  • yes,you answer is correct .The JAVAHOME is required.I edit the /etc/profile instead – Ninja Oct 13 '13 at 14:36
  • 8
    Well, adding to ~/.mavenrc ensures that other apps are not affected. Only mvn picks up this version of JDK. :) – gvaish Oct 14 '13 at 02:19
  • Thank you! Finally maven in Eclipse compiles again...! – jherranzm Apr 12 '14 at 06:11
  • 43
    export JAVA_HOME=$(/usr/libexec/java_home -v 1.7) – jla May 19 '14 at 11:21
  • 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. – Steve Cohen Aug 08 '14 at 23:09
  • 1
    .mavenrc solution worked perfectly for me. Also realised I only had JDK 1.8 installed and not JRE 1.8. Thanks Gaurav! – timothyclifford Jul 17 '15 at 10:24
  • Excellent. Other recommendations suggested setting /etc/mavenrc similarly. Just as a warning to anyone else: ~/.mavenrc appears to take precedence. – krex Sep 23 '15 at 15:58
  • This seems to work without `export` too (at least in Ubuntu). – Paŭlo Ebermann Jul 09 '16 at 19:48
  • 3
    If you are using jenv on a mac, it can be: ```export JAVA_HOME="$HOME/.jenv/versions/`jenv version-name`"``` for `~/.mavenrc` file. – Eddy Aug 24 '17 at 19:48
  • 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 – user2035039 Dec 08 '19 at 13:39
45

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.

CupawnTae
  • 14,192
  • 3
  • 29
  • 60
Oskar
  • 479
  • 4
  • 2
  • 1
    The CurrentJDK symlink trick doesn't seem to work any more with maven 3.3.1, but modifying ~/.mavenrc does. – Stefan L Mar 23 '15 at 12:48
41

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>
DEREK LEE
  • 531
  • 4
  • 10
  • 6
    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 Jan 26 '15 at 18:36
27

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.

user1231232141214124
  • 1,339
  • 3
  • 16
  • 22
  • o,thank you. but I haven't start . I just 'mvn -v'...the reason is I don't assign JAVA_HOME – Ninja Sep 16 '13 at 08:55
  • 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 –  Apr 28 '14 at 15:47
  • 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 Jan 26 '15 at 18:36
  • 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 – John Doe Feb 01 '17 at 11:28
9

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>
Sergey Luchko
  • 2,996
  • 3
  • 31
  • 51
4

For Eclipse Users. If you have a Run Configuration that does clean package for example.

In the Run Configuration panel there is a JRE tab where you can specify against which runtime it should run. Note that this configuration overrides whatever is in the pom.xml.

Yonatan Abrego
  • 123
  • 3
  • 11
2

I am late to this question, but I think the best way to handle JDK versions on MacOS is by using the script described at: http://www.jayway.com/2014/01/15/how-to-switch-jdk-version-on-mac-os-x-maverick/

Yuri
  • 185
  • 1
  • 13
2

Please check the compatibility. I struggled with mvn 3.2.1 and jdk 1.6.0_37 for many hours. All variables were set but was not working. Finally I upgraded jdk to 1.8.0_60 and mvn 3.3.3 and that worked. Environment Variables as following:

JAVA_HOME=C:\ProgramFiles\Java\jdk1.8.0_60 
MVN_HOME=C:\ProgramFiles\apache-maven\apache-maven-3.3.3 
M2=%MVN_HOME%\bin extend system level Path- ;%M2%;%JAVA_HOME%\bin;
YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
1

@MasterGaurav's solution works perfectly.

I normally put the Java switch statement into a zsh function:

alias java_ls='/usr/libexec/java_home -V 2>&1 | grep -E "\d.\d.\d[,_]" | cut -d , -f 1 | colrm 1 4 | grep -v Home'

function java_use() {
    export JAVA_HOME=$(/usr/libexec/java_home -v $1)
    echo export "JAVA_HOME=$(/usr/libexec/java_home -v $1)" > ~/.mavenrc
    export PATH=$JAVA_HOME/bin:$PATH
    java -version
}

You can run java_ls to get all of the available JVMs on your machine,
and then java_use 1.7 to use 1.7 for both Java and Maven.

Miles B Huff
  • 98
  • 1
  • 2
  • 9
Wei Qiu
  • 886
  • 9
  • 16