5

I have performed all the necessary steps like the following after referring other Stack overflow threads and online resources for installing / configuring maven. Here is what I did :

export M2_HOME=/usr/local/apache-maven/apache-maven-3.3.9

export M2=$M2_HOME/bin

My Java Home variable is also, set, as shown below:

echo $JAVA_HOME

/Library/Java/JavaVirtualMachines/jdk1.8.0_60/Contents/Home

export PATH=$M2:$PATH  

Inspite of doing all of the above, when I run mvn --version, I get Error: JAVA_HOME is not defined correctly. We cannot execute /Library/Java/JavaVirtualMachines/jdk1.8.0_60/Contents/Home/bin/java.

PATH Output is as follows :

 echo $PATH
/usr/local/apache-maven/apache-maven-3.3.9/bin:/usr/local/apache-maven/apache-maven-3.2.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/git/bin:/usr/local/MacGPG2/bin
DeltaG
  • 760
  • 2
  • 9
  • 28
John
  • 1,210
  • 5
  • 23
  • 51

2 Answers2

6

export PATH=$M2:$PATH should be export PATH=$M2/bin:$PATH or export PATH=$M2_HOME/bin:$PATH

Also JAVA_HOME should be something like this

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_60

then

export PATH=$JAVA_HOME/bin:$PATH

That's all.

All in one

export M2_HOME=/usr/local/apache-maven/apache-maven-3.3.9
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_60
export PATH=$JAVA_HOME/bin:$M2_HOME/bin:$PATH
11thdimension
  • 10,333
  • 4
  • 33
  • 71
  • I would also like to add as a side note that you can make your life a lot easier if you choose to use a package manager that can do most of the manual work for you in terms of installing and placing things in path. If you have some time - have a look on brew and brew cask . See [here](http://javapapo.blogspot.gr/2015/08/brew-and-brew-cask-package-manager-for.html) or [here](http://sourabhbajaj.com/mac-setup/Homebrew/Cask.html) – javapapo Jan 02 '16 at 09:17
  • @11thdimension Thanks for the answer and comment javapapo. My path variable is `echo $PATH /usr/local/apache-maven/apache-maven-3.3.9/bin:/usr/local/apache-maven/apache-maven-3.2.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/git/bin:/usr/local/MacGPG2/bin` I made the changes in `JAVA_HOME` variable as suggested. Still, I need to run `sudo mvn --version` to make sure that maven is running properly. If I just run `mvn --version` without `sudo`, I get the error `Error: JAVA_HOME is not defined correctly. We cannot execute /Library/Java/JavaVirtualMachines/jdk1.8.0_60/bin/java ` – John Jan 03 '16 at 01:36
  • Could anyone explain me why it throws `JAVA_HOME` error in spite of setting the path correctly? – John Jan 03 '16 at 01:37
  • Are you able to use Java commad in the terminal ? If you are able to use it, then try to run `which java`, which will give you the correct path to java executable. Post the output if you can find the path. – 11thdimension Jan 03 '16 at 01:41
  • @11thdimension When I ran the following command on the terminal:`ls -l `which java``, I got the following :` lrwxr-xr-x 1 root wheel 74 Jun 25 2015 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java` – John Jan 03 '16 at 02:45
  • 1
    what is the content of `/Library/Java/JavaVirtualMachines/jdk1.8.0_60/Contents/Home` directory ? – 11thdimension Jan 03 '16 at 02:52
0

I faced exactly the same problem and resolved it by getting the JAVA_HOME path by executing the following command: /usr/libexec/java_home -version

output: java_home: option `-version' requires an argument /Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home

Copy and paste this as value to JAVA_HOME and try again. It should work.