Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mvn -version error: We cannot execute /usr/libexec/java_home/bin/java

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

like image 407
Tai Avatar asked Jan 21 '15 22:01

Tai


2 Answers

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.

like image 172
RealSkeptic Avatar answered Nov 18 '22 00:11

RealSkeptic


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)

like image 24
Reza Dehnavi Avatar answered Nov 18 '22 02:11

Reza Dehnavi