Maven, JAVA_HOME is not defined correctly on Mac OSX

Apple recommends to set JAVA_HOME to “/usr/libexec/java_home“, for example :


$ vim .bash_profile 

export JAVA_HOME=/usr/libexec/java_home

$ source .bash_profile

But, when execute the Maven command, it prompts following error messages :


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

Solution

To fixed it, update the JAVA_HOME like following :


$ vim .bash_profile 

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

$ source .bash_profile

Run Maven again :


$ mvn -version

Apache Maven 3.0.3 (r1075438; 2011-03-01 01:31:09+0800)
Maven home: /usr/share/maven
Java version: 1.7.0_05, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.8.2", arch: "x86_64", family: "mac"

Done.

Reference

  1. Important Java Directories on Mac OS X
author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar
3000
newest oldest most voted
Manogna
Guest
Manogna

YAY!! it works thanks a ton!!!

saurabh
Guest
saurabh

thanks it worked out.

salim kocabas
Guest
salim kocabas

Thank you Mkyong, its very fast solution.

salim kocabas
Guest
salim kocabas

the quickest way, thank you mkyong.

Guest
Guest
Guest

Thanks a lot!

Brittany Neal
Guest
Brittany Neal

Thanks so much! Feeling like a config wizard now 😀

Priyekant Aghi
Guest
Priyekant Aghi

I tried following this.
But it did not work.
I am still getting this error
The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE

Please help me

prasad
Guest
prasad

In my case the problem was $HOME/.mavenrc file. JAVA_HOME was defined in that file. Easiest way to debug this kind of issues is adding some echo $JAVA_HOME logs to mvn script in /usr/local/bin/mvn

Michael Rodas
Guest
Michael Rodas

Thank you, it worked for me

Shubham Verma
Guest
Shubham Verma

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

Sabine Vogel
Guest
Sabine Vogel

wow you are a gift

Sinethemba
Guest
Sinethemba

Thanks this helped! 🙂

Sachin Hanumanthaiah
Guest
Sachin Hanumanthaiah

Hey i tried the above solution but still i am getting the below error after mvn clean install:
Error: Could not find or load main class org.codehaus.plexus.classworlds.launcher.Launcher

vrenjith
Guest
vrenjith

You are so amazing mkyong. The problems and crisp solutions that you give in your articles are simply great.

Brendan Heussler
Guest
Brendan Heussler

Thank you. This worked for me. I believe this works because /usr/libexec/java_home is a symbolic link to the actual JAVA_HOME location. Wrapping it in $() resolves the path during execution. Correct?

Martin Mauch
Guest
Martin Mauch

Not quite: java_home is an executable. When you run it, it prints the path to the Java installation to stdout. The $() is bash functionality that executes a command and returns what was printed to stdout.

ss
Guest
ss

hi,help me.

maven package javadoc? jdk path is jre ,no jdk.

error info:

MavenReportException: Error while creating archive: Unable to find javadoc command: The environment variable JAVA_HOME is not correctly set. -> [Help 1]

berryjam
Guest
berryjam

Thanks!It works.

Doug Bergh
Guest
Doug Bergh

I accidentally deleted my /lib/exec/java_home. I’d rather replace it than hard-set JAVA_HOME. What are the contents?

Steve
Guest
Steve

very nice. it worked for me on set MAVEN_HOME and JAVA_HOME

depicus
Guest
depicus

Perfect thanks

Antonio Galván
Guest
Antonio Galván

Thanks by the solution.

nkanter
Guest
nkanter

Thanks, worked at first try

Srini
Guest
Srini

mkyong is really great, this solution helped me

Ömer Hakan Bilici
Guest
Ömer Hakan Bilici

Still works on Yosemite. Thanks

Sue Ann Ioanis
Guest
Sue Ann Ioanis

Thank you! Worked

David
Guest
David

This was spot on. Thank you so much

hiabc
Guest
hiabc

Thanks for the useful tip

WebDriverTips.com
Guest
WebDriverTips.com

Thanks for this tip, it solved my problem!

Mario Armenta
Guest
Mario Armenta

Gracias esto soluciono mi problema al actualizar a OS X Mavericks. Un saludo!

Tên mi?n
Guest
Tên mi?n

Thanks ! very useful !like it

Staffan Palm
Guest
Staffan Palm

Thank you, this solved the issue for me when installing Maven on my machine.