After Installing Java JDK 7 For Mac OS X - mvn -version still shows java version 1.6.0_31

JavaMacosMavenJava 7

Java Problem Overview


Oracle released Java JDK 7 on April 26 for Mac OS X. I followed the install instructions and when I do java -version in a terminal window I get:

java version "1.7.0_04"
Java(TM) SE Runtime Environment (build 1.7.0_04-b21)
Java HotSpot(TM) 64-Bit Server VM (build 23.0-b21, mixed mode)

However when I do mvn -version in the terminal window I get:

Apache Maven 3.0.2 (r1056850; 2011-01-08 18:58:10-0600)
Java version: 1.6.0_31, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: en_US, platform encoding: MacRoman
OS name: "mac os x", version: "10.7.3", arch: "x86_64", family: "mac"

How can I get Maven to use the Java JDK 1.7.0_04?

Thanks for the help.

Java Solutions


Solution 1 - Java

Finally found the answer here:

http://www.adam-bien.com/roller/abien/entry/java_se_development_kit_7

You should use JAVA_HOME=$(/usr/libexec/java_home) instead on a Mac and then set the current jdk via "Java Preferences.app".

Set JAVA_HOME in ~/.profile

Solution 2 - Java

The problem is that the symbolic link "CurrentJDK" inside the versions of JavaVm.framework points to the old jdk, so when i used the following commands to set the CurrentJDK to the latest one (1.7.0_45) it works

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

reference: http://java.dzone.com/articles/installing-jdk-7-mac-os-x

Solution 3 - Java

To find the path from Java Preferences, try /usr/libexec/java_home -X

My Java7 entry looks like this:

<dict>
	<key>JVMArch</key>
	<string>x86_64</string>
	<key>JVMBundleID</key>
	<string>com.oracle.java.7u04.jdk</string>
	<key>JVMEnabled</key>
	<true/>
	<key>JVMHomePath</key>
	<string>/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home</string>
	<key>JVMIsBuiltIn</key>
	<false/>
	<key>JVMName</key>
	<string>Java SE 7</string>
	<key>JVMPlatformVersion</key>
	<string>1.7</string>
	<key>JVMVendor</key>
	<string>Oracle Corporation</string>
	<key>JVMVersion</key>
	<string>1.7.0_04</string>
</dict>

Solution 4 - Java

You need to change the JAVA_HOME environment variable to the new JDK 1.7 location.

Look at this question for possible locations where JAVA_HOME might be defined. In a terminal, type which java to find the path of your Java installation, and then update JAVA_HOME to point to that path (but exclude the trailing bin folder).

Solution 5 - Java

In case anyone is installing Maven on a 64-bit Mac running Mac OSX 'Mavericks' save yourself some time and some hair pulling trying to get Maven installed. I was trying to follow this (which failed because the location of the java_home has changed on Mavericks:

http://maven.apache.org/download.cgi

I was pulling my hair out until I found this website, which recommends you Install homebrew and follow the instructions on this page: http://techspunky.blogspot.in/2013/10/how-to-install-maven-on-mac-osx-109.html

once complete don't panic when $ maven -version doesn't work, instead use $mvn --version as maven.apache.org/download.cgi recommended.

It worked, but then I noticed the Java SDK Maven was using was Java version: 1.6.0_65, which is not the latest SDK I installed on the system.

Once I set the JAVA_HOME=/usr/libexec/java_home

It was using the correct SDK: Maven home: /usr/local/Cellar/maven/3.1.1/libexec Java version: 1.7.0_45, vendor: Oracle Corporation Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/jre Default locale: en_US, platform encoding: UTF-8 OS name: "mac os x", version: "10.9", arch: "x86_64", family: "mac"

Success! but wait... next time I opened the terminal and ran mvn -v it went back to java version 1.6. WTH!

its some sort of supposed bug from what I read use: echo JAVA_HOME=/usr/libexec/java_home -v 1.7 | sudo tee -a /etc/mavenrc

and now:

Maven home: /usr/local/Cellar/maven/3.1.1/libexec Java version: 1.7.0_45, vendor: Oracle Corporation Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/jre Default locale: en_US, platform encoding: UTF-8 OS name: "mac os x", version: "10.9", arch: "x86_64", family: "mac"

Solution 6 - Java

The reason Maven is still using Java 6 is that the /usr/bin/mvn script that launches it does not use the correct OS/X method for resolving the current Java version as specified in Java Preferences. See this Maven issue for details:

http://jira.codehaus.org/browse/MNG-4226

Voting it up might get it fixed; in the meantime, if you alter the /usr/bin/mvn script as shown in the comments & patch on that issue you will get the desired result.

Solution 7 - Java

Ok on my machine. Yes, the Oracle installer didn't exactly do its job, and it confused me to no end after 1.6 was still running.

java run time was updated 

java -version
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)

but the SDK HOME is actually a sim link still pointing to 1.6

/Library/Java/Home in finder GetInfo points to /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

in Terminal

./Home -> /System/Library/Frameworks/JavaVM.framework/Home

Go figure.

I found 1.7 here and I'm using that to build with: /Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home

Solution 8 - Java

I think the problem lies partly with the java launch mechanism. I have been able to get Eclipse to run my code under Java7, but I think eclipse itself is still running under Java6. Also, I have a java program that was last compiled a year ago, and built using jarbundler-2.1.0.jar (with option jvmversion="1.6+"). When I double click the .app file it runs Java6, but when I double click the .jar file inside the .app it runs Java7.

Also, I do not use JAVA_HOME, but I still get the correct java version "1.7.0_04" shown in the question. JAVA_HOME seems to be a red herring. Maybe Maven needs it, but Eclipse doesn't seem to use it, and neither does launching jar files.

And after setting Java7 in the Java Preferences tool, it does not seem to affect launching .app java files. However it does make 'java -version' from the terminal work nicely. I tried unchecking everything but the Java7 option, and then Eclipse wouldn't run at all.

Solution 9 - Java

If your JAVA_HOME is located at the local user's bash, mvn will pick the system's current java version no matter what JAVA_HOME you've set.

Hence first run the command 'which mvn' and check which executable mvn you are using. After that edit that mvn file and change the JAVA_HOME variable to your prefered java home.

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
QuestionBruce PhillipsView Question on Stackoverflow
Solution 1 - JavaBruce PhillipsView Answer on Stackoverflow
Solution 2 - JavaMahmoud AdamView Answer on Stackoverflow
Solution 3 - JavadmolonyView Answer on Stackoverflow
Solution 4 - JavaNateView Answer on Stackoverflow
Solution 5 - JavaConfusedDeerView Answer on Stackoverflow
Solution 6 - JavaRobert ElliotView Answer on Stackoverflow
Solution 7 - JavadaveView Answer on Stackoverflow
Solution 8 - JavadmolonyView Answer on Stackoverflow
Solution 9 - JavaAnkit BhatnagarView Answer on Stackoverflow