我无法在MacOS终端上运行Maven,因为它显示:
JAVA_HOME环境变量定义不正确,该环境变量需要运行此程序。
每一篇与该问题有关的Google搜索结果都说不要在你的export命令中加入空格...但事实并非如此简单...下面是一些输出内容:
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
我甚至将我的.bash_profile移动到另一个文件夹并开始了新的会话。我还尝试过使用brew uninstall然后使用brew install...但没有成功。
有什么想法吗?
编辑:
在mvn命令中,这是一个脚本,显然它失败了:
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
我复制了这一行
JAVACMD="$JAVA_HOME/bin/java"
然后使用它... 这是我的结果... 所以不知道为什么在脚本内部失败 - 但我对bash脚本的了解非常有限。
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)
我甚至将脚本修改为这样
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
这是我的输出结果
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.
接下来,我从if then语句中删除了负面测试,以使其通过,现在它显示如下:
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
我将检查那个Library文件夹中的文件夹权限。
$JAVA_HOME
,Maven不会抱怨。 - g00se