How to fix the maven error The JAVA_HOME environment variable is not defined correctly

丶灬走出姿态 提交于 2021-01-03 06:15:06

问题


When I check on maven's version on a virtual environment, I get the following errors:

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

However, I am able to print maven's version outside of virtual environment

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"

I have used set in ~/.bash_profile

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

I don't understand the error as JAVA_HOME is pointing to a JDK and not a JRE? Thanks for answering!


回答1:


This is not correct:

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

Notice that the output of mvn tells you the location of Java home is /Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk/Contents/Home/jre. So the location of the JDK home is probably /Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk/Contents/Home.

Secondly, you need to export the variable, not just set it.

Write like this:

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

After you update your ~/.bash_profile, to test that the setup is correct, start a new shell, and check the output of echo $JAVA_HOME.



来源:https://stackoverflow.com/questions/47243223/how-to-fix-the-maven-error-the-java-home-environment-variable-is-not-defined-cor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!