Why Maven uses JDK 1.6 but my java -version is 1.7
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>
Related videos on Youtube
![Ninja](https://i.stack.imgur.com/YpDz1.jpg?s=256&g=1)
Comments
-
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 almost 11 yearsCan you post your PATH and JAVA_HOME variables?
-
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 almost 11 years@JeanneBoyarsky is this you want,sir?
-
Jeanne Boyarsky almost 11 yearsI'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 almost 11 years@JeanneBoyarsky Forgive my rudeness.The url you given is the problem I meet.Thanks a lot~
-
-
Ninja almost 11 yearso,thank you. but I haven't start . I just 'mvn -v'...the reason is I don't assign JAVA_HOME
-
Ninja over 10 yearsyes,you answer is correct .The JAVAHOME is required.I edit the /etc/profile instead
-
gvaish over 10 yearsWell, adding to ~/.mavenrc ensures that other apps are not affected. Only mvn picks up this version of JDK. :)
-
jherranzm about 10 yearsThank you! Finally maven in Eclipse compiles again...!
-
Admin about 10 yearsNote 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 about 10 yearsexport JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
-
Steve Cohen almost 10 yearswow. 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 over 9 yearsThat 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 over 9 yearsThat 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 over 9 yearsThe CurrentJDK symlink trick doesn't seem to work any more with maven 3.3.1, but modifying ~/.mavenrc does.
-
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 almost 9 yearsExcellent. Other recommendations suggested setting /etc/mavenrc similarly. Just as a warning to anyone else: ~/.mavenrc appears to take precedence.
-
Paŭlo Ebermann almost 8 yearsThis seems to work without
export
too (at least in Ubuntu). -
John Doe over 7 yearsThis 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 almost 7 yearsIf you are using jenv on a mac, it can be:
export JAVA_HOME="$HOME/.jenv/versions/`jenv version-name`"
for~/.mavenrc
file. -
user2035039 over 4 yearsWhen using IntelliJ, it is also possible to set the JRE to be used by Maven via Settings > Build, Execution, Deployment > Build Tools > Maven > Runner