Error: JAVA_HOME is not defined correctly executing maven


I installed java and set path to environment and when I execute echo $JAVA_HOME I get the following output:


I Also installed apache-maven and changed environment file and now it looks like this:

MAVEN_OPTS="-Xms256m -Xmx512m"

But when I execute mvn --version I get a warning:

Error: JAVA_HOME is not defined correctly.
  We cannot execute /usr/lib/jvm/java-7-oracle/jre/bin/java/bin/java

Can not find out why it repeats in the end /bin/java/bin/java

This question is tagged with java maven java-home

~ Asked on 2014-12-05 15:40:19

The Best Answer is


Assuming you use bash shell and installed Java with the Oracle installer, you could add the following to your .bash_profile

export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/jre/bin:$PATH

This would pick the correct JAVA_HOME as defined by the Oracle installer and will set it first in your $PATH making sure it is found.

Also, you don't need to change it later when updating Java.


As per the comments:

Making it persistent after a reboot

Just add those lines in the shell configuration file. (Assuming it's bash)

Ex: .bashrc, .bash_profile or .profile (for ubuntu)

Using a custom Java installation

Set JAVA_HOME to the root folder of the custom Java installation path without the $().

Ex: JAVA_HOME=/opt/java/openjdk

~ Answered on 2015-02-24 01:48:52


JAVA_HOME should be /usr/lib/jvm/java-7-oracle/jre/.

~ Answered on 2014-12-05 15:42:01

Most Viewed Questions: