brew安装maven后无法使用mvn命令

在使用了
brew install maven之后,想看看maven的信息,于是输入mvn -version,但是zsh提示

The JAVA_HOME environment variable is not defined correctly,
this environment variable is needed to run this program.

于是想着去看看JAVA_HOME到底有没有被设置,输入
echo ${JAVA_HOME} 输出结果为一个空行,说明没有设置JavaHome,
于是按照如下的步骤设置Java home

  1. 查看可用的jdk
    /usr/libexec/java_home -V
    输出了
Matching Java Virtual Machines (1):
    11.0.16.1 (arm64) "Homebrew" - "OpenJDK 11.0.16.1" /opt/homebrew/Cellar/openjdk@11/11.0.16.1_1/libexec/openjdk.jdk/Contents/Home
/opt/homebrew/Cellar/openjdk@11/11.0.16.1_1/libexec/openjdk.jdk/Contents/Home

说明当前jdk11是可用的
2. 在~/.zshrc中加入JavaHome的配置

export JAVA_HOME=$(/usr/libexec/java_home)
  1. 更新zshrc
    source ~/.zshrc
  2. 再次输出Java Home
    ${JAVA_HOME}
    这次控制台不是输出空行了,而是
/opt/homebrew/Cellar/openjdk@11/11.0.16.1_1/libexec/openjdk.jdk/Contents/Home

说明javahome已经配置到zshrc里面了
5. 使用mvn命令
mvn -version
输出了

Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)
Maven home: /opt/homebrew/Cellar/maven/3.8.6/libexec
Java version: 11.0.16.1, vendor: Homebrew, runtime: /opt/homebrew/Cellar/openjdk@11/11.0.16.1_1/libexec/openjdk.jdk/Contents/Home
Default locale: zh_CN_#Hans, platform encoding: UTF-8
OS name: "mac os x", version: "12.3", arch: "aarch64", family: "mac"
zcool@zcooldeMacBook-Pro-3 ~ % echo $JAVA_HOME
/opt/homebrew/Cellar/openjdk@11/11.0.16.1_1/libexec/openjdk.jdk/Contents/Home

至此,成功解决

你可能感兴趣的:(maven,java,jvm)