如何修复maven错误未正确定义JAVA_HOME环境变量

时间:2017-11-11 22:00:23

标签: java maven

当我在虚拟环境中检查maven的版本时,我收到以下错误:

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

但是,我能够在虚拟环境之外打印maven的版本

mvn -version
Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T00:58:13-07:00)
Maven home: /usr/local/Cellar/maven/3.5.2/libexec
Java version: 1.8.0_152, vendor: Oracle Corporation
Java home:  /Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.11.6", arch: "x86_64", family: "mac"

我在〜/ .bash_profile

中使用了set
set JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk
set PATH=$JAVA_HOME/jre/bin:$PATH

我不明白错误,因为JAVA_HOME指向JDK而不是JRE?谢谢你的回答!

1 个答案:

答案 0 :(得分:3)

这是不正确的:

set JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk

请注意,mvn的输出告诉您Java home的位置为/Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk/Contents/Home/jre。 因此JDK主页的位置可能是/Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk/Contents/Home

其次,您需要export变量,而不仅仅是set

像这样写:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk/Contents/Home

更新~/.bash_profile后, 测试设置是否正确, 启动一个新shell,并检查echo $JAVA_HOME的输出。