Error: JAVA_HOME is not defined correctly executing maven
I installed java and set the path environment and when I run echo $JAVA_HOME
in the terminal I get the following output:
/usr/lib/jvm/java-7-oracle/jre/bin/java
I Also installed apache-maven
and changed environment
file and now it looks like this:
JAVA_HOME="/usr/lib/jvm/java-7-oracle/jre/bin/java"
M2_HOME=/usr/local/apache-maven/apache-maven-3.0.5
M2=$M2_HOME/bin
MAVEN_OPTS="-Xms256m -Xmx512m"
PATH=$M2:$PATH
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
Solution 1:
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.
EDIT
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
Solution 2:
JAVA_HOME should be /usr/lib/jvm/java-7-oracle/jre/
.
Solution 3:
We open a terminal and look for the location of java:
manuel@zonademanel:~ → whereis java
java: /usr/bin/java /etc/java /usr/bin/X11/java /usr/share/java /usr/share/man/man1/java.1.gz
What we are looking for is /usr/bin/java continue on the command line to find the absolute path , as this is only a symbolic link.
manuel@zonademanel:~ → ls -lah /usr/bin/java
lrwxrwxrwx 1 root root 22 may 19 2015 /usr/bin/java -> /etc/alternatives/java
manuel@zonademanel:~ → ls -lah /etc/alternatives/java
lrwxrwxrwx 1 root root 39 dic 7 11:52 /etc/alternatives/java -> /usr/lib/jvm/java-8-oracle/jre/bin/java
I modified my /etc/environment file with the following values
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/java-8-oracle/jre/bin" JAVA_HOME="/usr/lib/jvm/java-8-oracle/jre"
If I do not want to relogin I can reload the changes with:
source /etc/environment
And run mvn -version correctly
manuel@zonademanel:~ → mvn -version
Apache Maven 3.0.5
Maven home: /usr/share/maven
Java version: 1.8.0_77, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-oracle/jre
Default locale: es_MX, platform encoding: UTF-8
OS name: "linux", version: "3.16.0-70-generic", arch: "amd64", family: "unix"