'Maven The JAVA_HOME environment variable is not defined correctly - even though it looks right
I cannot run Maven from terminal on MacOS because it says
The JAVA_HOME environment variable is not defined correctly, this environment variable is needed to run this program.
Every Google hit on the problem says not to have a space in your export command ... I only wish it were that simple ... here is some output
export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_311.jdk/Contents/Home'
mvn -version
The JAVA_HOME environment variable is not defined correctly,
this environment variable is needed to run this program.
ls $JAVA_HOME
COPYRIGHT THIRDPARTYLICENSEREADME.txt jmc.txt man
LICENSE bin jre release
README.html include legal src.zip
THIRDPARTYLICENSEREADME-JAVAFX.txt javafx-src.zip lib
$JAVA_HOME/bin/java -version
java version "1.8.0_311"
Java(TM) SE Runtime Environment (build 1.8.0_311-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.311-b11, mixed mode)
michael on Mikes-MacBook-Pro (c for commands)
mvn
The JAVA_HOME environment variable is not defined correctly,
this environment variable is needed to run this program.
export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk-17.0.2.jdk/Contents/Home'
echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk-17.0.2.jdk/Contents/Home
export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_311.jdk/Contents/Home'
echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_311.jdk/Contents/Home
I even moved my .bash_profile to another folder and started a new session. I also tried a brew uninstall then a brew install ... with no luck.
Any ideas?
Edit:
Inside the mvn command, which is a script, here is where it is apparently failing:
if [ -z "$JAVA_HOME" ] ; then
JAVACMD="`\\unset -f command; \\command -v java`"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
if [ ! -x "$JAVACMD" ] ; then
echo "The JAVA_HOME environment variable is not defined correctly," >&2
echo "this environment variable is needed to run this program." >&2
exit 1
fi
I copied this line
JAVACMD="$JAVA_HOME/bin/java"
and used it ... this was my results ... so no idea why it's failing inside the script - but my knowledge of bash script is very limited.
JAVACMD="$JAVA_HOME/bin/java"
$JAVACMD -version
java version "1.8.0_311"
Java(TM) SE Runtime Environment (build 1.8.0_311-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.311-b11, mixed mode)
I even modified the script to this
echo $JAVACMD
if [ ! -x "$JAVACMD" ] ; then
echo "The JAVA_HOME environment variable is not defined correctly," >&2
echo "this environment variable is needed to run this program." >&2
exit 1
fi
And this is my output
mvn
/Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home/bin/java
The JAVA_HOME environment variable is not defined correctly,
this environment variable is needed to run this program.
Next, I took out the negative test in the if then statement so it would pass and now its saying this:
mvn
/usr/local/Cellar/maven/3.8.5/libexec/bin/mvn: line 194: /Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home/bin/java: No such file or directory
/usr/local/Cellar/maven/3.8.5/libexec/bin/mvn: line 194: exec: /Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home/bin/java: cannot execute: No such file or directory
Im going to check folder permissions in that Library folder.
Solution 1:[1]
Ultimately, the problem was that the mvn command (which again is a script, not a compiled program) makes a call to $HOME/.mavenrc and inside of that file, was a different path assignment for $JAVA_HOME which was identical to my path, but instead of referencing jdk17.0.2, it was trying to reach jdk17.0.1 and I had uninstalled that jdk long enough prior to having this problem that I had forgotten I removed it.
Nonetheless, one expects a command that one executes to rely on live environment variables that are in place and not to refer to some other file that has a re-definition for an existing environment variable.
I blame Maven for not having their script first check the live environment for the paths it needs before deferring to one that was defined in the past.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Michael Sims |
