Recently I switched to Java 7 and then ran into a couple of issues. I had a Mac with OS X Mountain Lion (10.8.2) and Maven 3.0.4 (Maven installed using “brew install maven”). Both Java 6 and Oracle Java 7 was installed on the system with the default JVM set to Java 7:
$ java -version
java version "1.7.0_11" Java(TM) SE Runtime Environment (build 1.7.0_11-b21) Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode)
But Maven was picking up Java 6:
$ mvn -version
Apache Maven 3.0.4 (r1232337; 2012-01-17 09:44:56+0100) Maven home: /usr/local/Cellar/maven/3.0.4/libexec Java version: 1.6.0_41, 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.8.2", arch: "x86_64", family: "mac"
The JAVA_HOME environment variable was set in ~/.profile:
export MAVEN_OPTS="-Xmx512m -XX:MaxPermSize=128M" export JAVA_HOME=`/usr/libexec/java_home -v 1.7`
but it was not exported. It turned out that brew had created a ~/.bash_profile file and the bash shell (from the bash man page):
looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.
To use the settings in ~/.profile I could had moved them to .~/bash_profile but I decided to keep them in ~/.profile and instead load it from ~/.bash_profile using:
After fixing the shell initialization files Maven was picking up Java 7 as expected.
Now I tried to launch mvn from within eclipse but got “Fatal error compiling: invalid target release: 1.7″. Eclipse was started from a shortcut to Eclipse.app rather than a shell so the JAVA_HOME variable was not set. I googled and found a workaround in http://jira.codehaus.org/browse/MNG-4226. It turned out that Maven loads settings from /etc/mavenrc if that file exists. I didn’t have that file on my system so I created it with:
echo JAVA_HOME=`/usr/libexec/java_home -v 1.7` | sudo tee -a /etc/mavenrc
Finally Maven was picking up Java 7 both when run from the shell and from eclipse.
If you’re are facing problems with finding JAVA_HOME you might also want to check this article on Stack Overflow: Where is JAVA_HOME on OSX Lion?.