MAC解决The JAVA_HOME environment variable is not defined correctly

安装好jdk,在.bash_profile中配置好java_home,path变量:

export PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home

这时在终端运行”java -version",可得到如下结果:

java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

然后,按照maven官网上的,将

export PATH=/opt/apache-maven-3.5.4/bin:$PATH

加到.bash_profile中,却总是报

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解压到任意文件夹,(ps:比如我放在development之下,并且修改配置为“export PATH=/xxx/xxx/development/apache-maven-3.5.4/bin:$PATH”,但是不行!)找了很多网页,也没有遇到相同的问题
最后,一字不差地按照官网来,将maven的解压目录放在

‎⁨Macintosh HD⁩/⁨opt⁩

目录下面才能解决问题!
注意,如果电脑没有opt目录,自己新建一个即可,此opt目录是与“应用程序”,“用户”等目录同级的,然后

mvn -v

得到正确的结果:

Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-18T02:33:14+08:00)
Maven home: /opt/apache-maven-3.5.4
Java version: 1.8.0_191, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.14", arch: "x86_64", family: "mac"

最后,贴上全部的配置:
~/.bash_profile
(此文件路径为“users/用户名/.bash_profile”,个人理解,“~/”即是指当前用户目录,如果没有,新建即可)

export PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home
export PATH=/opt/apache-maven-3.5.4/bin:$PATH

以下为maven目录位置:


MAC解决The JAVA_HOME environment variable is not defined correctly_第1张图片
maven.png

dmg格式的jdk安装位置为默认在Library(中文名为‘资源库’下),不再附图

修改.bash_profile后,记得

source ~/.bash_profile

如果用的是zsh:
需要在~/.zshrc最后一行加入

source ~/.bash_profile

修改后,同样重新载入配置

source ~/.zshrc

你可能感兴趣的:(MAC解决The JAVA_HOME environment variable is not defined correctly)