JAVA_HOME error while maven build, OSX-10.8.4 Version

Error: JAVA_HOME is not defined correctly.
  We cannot execute
  /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/bin/java

I have upgraded my OSX to 10.8.4. After OSX upgrade I have upgraded my JDK and JRE to 7. But I am getting the above error when I am trying for maven build of project.

When I run ls -al /System/Library/Frameworks/JavaVM.framework/Versions/

I get the below response

drwxr-xr-x  11 root  wheel  374 Jul 22 22:33 .
drwxr-xr-x  10 root  wheel  340 Jul 22 18:30 ..
lrwxr-xr-x   1 root  wheel   10 Jul 22 18:30 1.4 -> CurrentJDK
lrwxr-xr-x   1 root  wheel   10 Jul 22 18:30 1.4.2 -> CurrentJDK
lrwxr-xr-x   1 root  wheel   10 Jul 22 18:30 1.5 -> CurrentJDK
lrwxr-xr-x   1 root  wheel   10 Jul 22 18:30 1.5.0 -> CurrentJDK
lrwxr-xr-x   1 root  wheel   10 Jul 22 18:30 1.6 -> CurrentJDK
lrwxr-xr-x   1 root  wheel   10 Jul 22 18:30 1.6.0 -> CurrentJDK
drwxr-xr-x   7 root  wheel  238 Jul 22 18:30 A
lrwxr-xr-x   1 root  wheel    1 Jul 22 18:30 Current -> A
lrwxr-xr-x   1 root  wheel   59 Jul 22 18:30 CurrentJDK -> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents

For this ls -al /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/bin

I don't see any contents in CurrentJDK further.

On running /usr/libexec/java_home

I get:

/Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home

I am pretty much new to working with OS X. Let me know if I might have missed any point still in making it successful.


Solution 1:

Apple recommends to set JAVA_HOME to “/usr/libexec/java_home“.

What worked for me was wrapping this around $(). So I ended up configuring JAVA_HOME like $(/usr/libexec/java_home) with the following commands:

vim ~/.bash_profile 
export JAVA_HOME=$(/usr/libexec/java_home)
source ~/.bash_profile

Solution 2:

/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/bin/java if it exists is the Java supplied by Apple of which the latest version would be a JRE 1.6.

To use the Oracle JDK (the java 7 you mention) use the path /usr/bin/java if you have installed it fully. Note that the JRE is only for Java applets and so does not affect /usr/bin/java

For more information see the Oracle install documentation

and note from there

To run a different version of Java, either specify the full path, or use the java_home tool:

/usr/libexec/java_home -v 1.7.0_06 --exec javac -version

For more information, see the java_home(1) man page.

Solution 3:

Follow this link : https://stackoverflow.com/a/44564303/5005676 If you see the mvn script: The code fails here ---

Steps for debugging and fixing:

Step 1: Open the mvn script /Users/Username/apache-maven-3.0.5/bin/mvn (Open with the less command like: less /Users/Username/apache-maven-3.0.5/bin/mvn)

Step 2: Find out the below code in the script:

  if [ -z "$JAVACMD" ] ; then
  if [ -n "$JAVA_HOME"  ] ; then
    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
      # IBM's JDK on AIX uses strange locations for the executables
      JAVACMD="$JAVA_HOME/jre/sh/java"
    else
      JAVACMD="$JAVA_HOME/bin/java"
    fi
  else
    JAVACMD="`which java`"
  fi
fi

if [ ! -x "$JAVACMD" ] ; then
  echo "Error: JAVA_HOME is not defined correctly."
  echo "  We cannot execute $JAVACMD"
  exit 1
fi

Step3: It is happening because JAVACMD variable was not set. So it displays the error.

Note: To Fix it

export JAVACMD=Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/bin/java

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/

Key: If you want it to be permanent open emacs.profile

post the commands and press Ctrl-x Ctrl-c ( save-buffers-kill-terminal ).