118

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.

dmolony
  • 1,125
  • 9
  • 23
Bruce Phillips
  • 2,740
  • 3
  • 18
  • 17

9 Answers9

128

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

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
Bruce Phillips
  • 2,740
  • 3
  • 18
  • 17
  • 4
    I recently installed Java 6 and I am not able to locate JavaPreferences.app bcz mac now does not provide support for Java. Can tell me now how can I set my jvm without using JavaPreferences. – amod Feb 11 '13 at 06:41
  • 2
    You can accept your own answer. That will show all the rest of us that this solved this problem. And you will have 10 more ponts :) – Ibolit Mar 17 '13 at 10:58
  • 5
    I added to ~/.bash_profile: export JAVA_HOME=$(/usr/libexec/java_home) – skytteren Mar 22 '13 at 10:36
  • 1
    Or you can do "export JAVA_HOME=$(/usr/libexec/java_home -v 1.6)" in one shot instead of changing using Java Preferences.app – mac Jul 30 '15 at 14:46
  • unable to open the Java Preferences App in El Captian, I went from System Preferences - > Java. – Wolf7176 Aug 29 '16 at 14:54
34

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

Mahmoud Adam
  • 5,772
  • 5
  • 41
  • 62
17

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>
dmolony
  • 1,125
  • 9
  • 23
10

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).

Community
  • 1
  • 1
Nate
  • 557
  • 3
  • 8
  • Are you sure I need to that on a Mac OS X? I've not had to do that in the past. If I do need to set JAVA_HOME how do I know what path to use as the install of Java JDK 7 did not give me any install path information and the Java Preference utility app on the Mac doesn't show a path to the JDK 1.7 – Bruce Phillips Apr 26 '12 at 19:46
  • If you look at your question above, you can see that JAVA_HOME has already been defined and currently points to the old 1.6 JDK. I'm not familiar with how to change environment variables in OSX, but this is what Maven uses by default. – Nate Apr 26 '12 at 20:32
  • The problem is that an Apple update normally updates the java version and sets the JAVA_HOME value. Setting the JAVA_HOME value in .bashrc doesn't work on OS X as it does in linux. Also no where can I find the path to the JDK 7 installed by Oracle. – Bruce Phillips Apr 26 '12 at 21:55
  • Also Nate my question is NOT a duplicate of the post you linked to. I'm on OS X and have updated to Java JDK 7 using Oracle install for Mac OS X, which was just released on April 26. – Bruce Phillips Apr 26 '12 at 21:59
  • No Mac instead of .bashrc uses .bash_profile and you should be able to find instalation under /System/Library/Java/JavaVirtualMachines/ where you need to point on your new 1.7.0 Java version – peter_budo Apr 27 '12 at 00:12
  • Peter - there is only 1 JDK listed under that directory and it is not the new JDK 1.7. siteamnotebook1-2:~ bphillips$ cd /System/Library/Java/JavaVirtualMachines/ siteamnotebook1-2:JavaVirtualMachines bphillips$ ls -l total 0 drwxr-xr-x 3 root wheel 102 Nov 1 12:49 1.6.0.jdk – Bruce Phillips Apr 27 '12 at 15:14
  • Nate - using which java in a terminal just returns /usr/bin/java and that is not the path to the JDK 1.7 – Bruce Phillips Apr 27 '12 at 15:16
7

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"

ConfusedDeer
  • 3,335
  • 8
  • 44
  • 72
4

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.

Robert Elliot
  • 1,372
  • 13
  • 20
2

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

Lucas Zamboulis
  • 2,494
  • 5
  • 24
  • 27
dave
  • 21
  • 1
1

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.

Ankit Bhatnagar
  • 745
  • 5
  • 16
0

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.

dmolony
  • 1,125
  • 9
  • 23