6

====== EDIT 2:

I used the same code as mentioned below in edit 1, my error apparently was not in the code itself but rather due to source ~/.bash_profile not updating my file as expected. I think, however (thank you @eckes) please see the maven bug:( BTW: jira.codehaus.org/browse/MNG-5686 – eckes 39 secs ago)

EDIT 1: this is not a duplicate of: What should I set JAVA_HOME to on OSX

I have tried all of the solutions mentioned in the above post, and they do not work for me. My issue is that I am doing one of the solutions listed there here (I tried the others). I am still getting the error.

I'm currently trying as per above instructions:

  export JAVA_HOME=$(/usr/libexec/java_home)

and getting:

  source ~/.bash_profile
  mvn -version
Error: JAVA_HOME is not defined correctly.
  We cannot execute /usr/libexec/java_home/bin/java

======= I installed the latest java jdk from oracle

java -version
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)

I installed Xcode to install maven however recent documentation states that I may use brew (Maven Install on Mac OS X)

I installed maven with:

//install brew
brew doctor
brew install maven

I then tried:

mvn -version
     Error: JAVA_HOME is not defined correctly.

I found: http://hathaway.cc/post/69201163472/how-to-edit-your-path-environment-variables-on-mac

However this is based on java 7 I believe. But it seemed likely to be a fix.

There is no bash_profile on the new mac (https://apple.stackexchange.com/questions/26928/no-bash-profile-in-os-x-lion)

So I created and edited my bash_profile with:

touch ~/.bash_profile
vim ~/.bash_profile
//and added the line export JAVA_HOME=(/usr/libexec/java_home)

My problem is that even with this fix I am still getting the following error:

 mvn -version
      Error: JAVA_HOME is not defined correctly.
      We cannot execute /usr/libexec/java_home/bin/java

I checked however that within my file system java_home is where the file lies. There is no "/usr/libexec/java_home/bin/java" in my file system as maven error'ed out on.

Has anyone else encountered this with the new java 8, and brew installed maven? I'm using a MacBook Pro (Retina, 13-inch, Mid 2014) running Yosemite. I've installed a setup like this before but never encountered this issue.

**Note:

I found this: http://www.mkyong.com/java/maven-java_home-is-not-defined-correctly-on-mac-osx/

Which I thought would definitely be my fix. Correct me if I'm wrong but I believe the difference is the parenthesis.

Ex:

 export JAVA_HOME=/usr/libexec/java_home

vs

 export JAVA_HOME=(/usr/libexec/java_home)

However my error has not been fixed. (Also just in case anyone asks I source(ed) it before rerunning.) It did however change my error. It removed the additional /bin/java.

 mvn -version
      Error: JAVA_HOME is not defined correctly.
      We cannot execute /usr/libexec/java_home/bin/java

JAVA_HOME:

 echo $JAVA_HOME
      /Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home

**note i tried the $() syntax and this did not fix the problem.

Community
  • 1
  • 1
Tai
  • 1,206
  • 5
  • 23
  • 48
  • Did you insert the spaces in `export JAVA_HOME = /usr/libexec/java_home`? Can you open a Terminal window and do `echo $JAVA_HOME`, and tell us the result? – RealSkeptic Jan 21 '15 at 22:46
  • echo $JAVA_HOME /usr/libexec/java_home. There are no spaces. it's JAVA_HOME=(/usr/.../java_home) – Tai Jan 21 '15 at 22:48
  • I guess the backtick shell syntax (replacing the value with the output of the command) was lost in your sources. The $() is the alternative syntax: `JAVA_HOME=\`java_home\`` – eckes Jan 21 '15 at 23:16
  • @eckes wait sorry could you explain that? I switched it to $() and it still isn't working correctly? – Tai Jan 21 '15 at 23:18
  • both is correct. You just need to make sure you run the command in your current session and export it. Your error message shows that mvn stil lsees the old value. But for a begin, just set it to a fixed string and try it. if this does not work you have another problem (for example the mvn script is modified or similar). – eckes Jan 21 '15 at 23:20
  • @eckes I run source ~/.bash_profile, and then I try mvn -version and I am still getting : JAVA_HOME is not defined correctly. We cannot execute /usr/libexec/java_home/bin/java. This is with the correct $() syntax – Tai Jan 21 '15 at 23:25
  • Then check if ~/.mavenrc or /etc/mavenrc overwrites the variable. You can also use `MAVEN_SKIP_RC=true mvn -v` (one line) to verify it is one of those files. – eckes Jan 21 '15 at 23:28
  • I ran the line : MAVEN_SKIP_RC=true mvn -v Error: JAVA_HOME is not defined correctly. We cannot execute /usr/libexec/java_home/bin/java Also ~/.mavenrc does not yet exist as a file – Tai Jan 21 '15 at 23:29
  • Then you can run `bash -x mvn` - it will print a lot of stuff (one line starting with + for every command) but you should see how JAVA_HOME gets assigned with a wrong value. – eckes Jan 21 '15 at 23:32
  • ... + '[' -x /usr/libexec/java_home/jre/sh/java ']' + JAVACMD=/usr/libexec/java_home/bin/java + '[' '!' -x /usr/libexec/java_home/bin/java ']' + echo 'Error: JAVA_HOME is not defined correctly.' Error: JAVA_HOME is not defined correctly. + echo ' We cannot execute /usr/libexec/java_home/bin/java' We cannot execute /usr/libexec/java_home/bin/java + exit 1 – Tai Jan 21 '15 at 23:33
  • I'll go with the benefit of the doubt here. It reads like the solution in the duplicate would work for you. Double-check your permissions; make sure that you can actually execute the binary. – Makoto Jan 21 '15 at 23:34
  • actually there is a bug in the mvn script, it needs to use $() as well. however it should not reach that line if JAVA_HOME was correctly set before. Anyway, change the `JAVA_HOME=/usr/libexec/java_home` in that script into $() version and it should work – eckes Jan 21 '15 at 23:35
  • @Makoto hey, thanks for the benefit of the doubt. I read the solution on the other page. It isn't working for me. It's a new computer hmm maybe ill check permissions. Weird – Tai Jan 21 '15 at 23:36
  • @eckes hmm ok wait my bash_profile is: export JAVA_HOME=$(/usr/libexec/java_home) which is correct yes? You want me to change which script to $() format? – Tai Jan 21 '15 at 23:37
  • @eckes OH, interesting. So I was running source ~/.bash_profile because I thought that that would refresh the file / location. I opened another window and it started running. The error I believe I was having was that source... was not update the file as I expected. A new window runs fine. Thank you so much for the help. My *solution* was creating a new terminal page. Any reasons why source .... doesn't work? – Tai Jan 21 '15 at 23:42
  • @Makoto you're correct the code required is a duplicate. I'm unsure of why I was having the above issue of source ~/.bash_profile not updating and allowing the file to run. I had to restart open terminal. Feel free to remove. Sorry! – Tai Jan 21 '15 at 23:46
  • BTW: http://jira.codehaus.org/browse/MNG-5686 – eckes Jan 21 '15 at 23:48

4 Answers4

29

You missed on one little thing, I believe:

export JAVA_HOME=$(/usr/libexec/java_home)
                 ⬆︎
          This dollar sign

This tells the shell "The value of this expression is the output from executing the following command (/usr/libexec/java_home))"

Running the command gives the proper java home for the mac (if you have a single JDK installed), and that value is placed in JAVA_HOME. Without the dollar sign, it just puts the name of the command there.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
  • Oh, awesome. I fixed that. I now get echo $JAVA_HOME /Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home, however maven still isn't running: mvn -version Error: JAVA_HOME is not defined correctly. We cannot execute /usr/libexec/java_home/bin/java – Tai Jan 21 '15 at 23:12
  • Did you export it? or did you actually logged in again (or sources .bashrc). – eckes Jan 21 '15 at 23:17
  • 1
    Are you running mvn in a different tab or terminal window? – RealSkeptic Jan 21 '15 at 23:41
  • I owe you a box of candy. – Phil Hudson Mar 06 '15 at 21:46
3

I write on .bash_profile file these lines and then work for me.

export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=/Users/(YOUR USER)/Documents/apache-maven-3.0.3/bin:$PATH
export PATH=$JAVA_HOME/jre/bin:$PATH

Note: work on macOS Sierra (10.12.6)

Reza Dehnavi
  • 2,256
  • 3
  • 16
  • 30
0

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

Shubham Verma
  • 997
  • 1
  • 10
  • 13
0

In my case, I had two entries for JAVA_HOME in my ~/.bash_profile and the last one was taking precedence. I just deleted the one which was wrong and it worked.

Pankaj Avhad
  • 344
  • 2
  • 3