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

  • Can you run echo $JAVA_HOME, ls -al /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/bin and ls -al /System/Library/Frameworks/JavaVM.framework/Versions/ and add the result to the question? – nohillside Jul 22 '13 at 14:28
  • @patrix .bashrc -> export JAVA_HOME=/usr/libexec/java_home .profile -> export PATH=${JAVA_HOME}/bin:$PATH export CATALINA_HOME=/Library/Tomcat/Home .bashrc and .profile has the above commands mentioned in it.. – arvind406 Jul 23 '13 at 4:13
  • @Patrix.. I see that the java path is not retrieved from bashrc file. When I set JAVA_HOME from terminal separately only then I could able to build a project. Why could this be happening like this ? Any idea ? – arvind406 Jul 23 '13 at 5:21
9

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
| improve this answer | |
  • This worked for me perfectly! – rdegges Feb 4 '15 at 17:44
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.

| improve this answer | |
  • .profile ->export PATH=${JAVA_HOME}/bin:$PATH export CATALINA_HOME=/Library/Tomcat/Home – arvind406 Jul 22 '13 at 18:00
  • @arvind what do those comments mean? – mmmmmm Jul 22 '13 at 18:15
  • @arvind406 yes but what does that add or ask about my answer? – mmmmmm Jul 22 '13 at 20:15
  • previous comments were commented in wrong window. Apologies. After upgrading to 10.8 OSX I have upgraded the JDK and JRE to 7. I have updated the question once again. kindly have a look and let me know if I am going wrong anywhere. On running this command /usr/libexec/java_home I get /Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home – arvind406 Jul 23 '13 at 4:17
  • @Mark.. I see that the path is not retrieved from bashrc file. When I set JAVA_HOME from terminal separately only then I could able to build a project. Why could this be happening like this ? Any idea ? – arvind406 Jul 23 '13 at 5:20
1

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

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .