Mac 自 macOS 10.12 Sierra 后不在自带 jdk 环境,但是保留了 java 命令(/usr/bin/java)
panminxiang@Mac ~ % which java
/usr/bin/java
测试下 java 是否正常
panminxiang@Mac ~ % java
The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.
panminxiang@Mac ~ % /usr/libexec/java_home
The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.
提示说缺少 jre 环境,jre 在 jdk 中,也就是需要下载 jdk
下载地址:
jdk21:https://www.oracle.com/java/technologies/downloads/#jdk21-mac
jdk8:https://www.oracle.com/java/technologies/downloads/#java8-mac (需要登录 oracle)
下载文件名:
jdk21:jdk-21_macos-aarch64_bin.dmg
jdk8:jdk-8u411-macosx-aarch64.dmg
安装 jdk21,安装完成后
panminxiang@Mac ~ % java -version
java version "21.0.3" 2024-04-16 LTS
Java(TM) SE Runtime Environment (build 21.0.3+7-LTS-152)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.3+7-LTS-152, mixed mode, sharing)
panminxiang@Mac ~ % /usr/libexec/java_home -V
Matching Java Virtual Machines (1):
21.0.3 (arm64) "Oracle Corporation" - "Java SE 21.0.3" /Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home
panminxiang@Mac ~ %
接着安装古早的 jdk8
安装完成后:
panminxiang@Mac ~ % /usr/libexec/java_home -V
Matching Java Virtual Machines (3):
21.0.3 (arm64) "Oracle Corporation" - "Java SE 21.0.3" /Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home
1.8.411.09 (arm64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
1.8.0_411 (arm64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk-1.8.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home
jdk8 携带了 java applet,系统会多出许多和 applet 相关的东西
系统会多出:
panminxiang@Mac Internet Plug-Ins % pwd
/Library/Internet Plug-Ins
panminxiang@Mac Internet Plug-Ins % ls
JavaAppletPlugin.plugin
panminxiang@Mac PreferencePanes % pwd
/Library/PreferencePanes
panminxiang@Mac PreferencePanes % ls
JavaControlPanel.prefPane
panminxiang@Mac Oracle % pwd
/Library/Application Support/Oracle
panminxiang@Mac Oracle % ls
Java
panminxiang@Mac Oracle % pwd
/Users/panminxiang/Library/Application Support/Oracle
panminxiang@Mac Oracle % ls
Java
java 真正安装的目录
panminxiang@Mac JavaVirtualMachines % pwd
/Library/Java/JavaVirtualMachines
panminxiang@Mac JavaVirtualMachines % ls
jdk-1.8.jdk jdk-21.jdk
panminxiang@Mac ~ % java -version
java version "21.0.3" 2024-04-16 LTS
Java(TM) SE Runtime Environment (build 21.0.3+7-LTS-152)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.3+7-LTS-152, mixed mode, sharing)
当前没有配置 $JAVA_HOME,则 java 命令来自于 /usr/libexec/java_home 的默认指向,即/Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home. /usr/libexec/java_home 默认指向版本最高的 jdk 路径。
切换 jdk
目前安装了以下版本:
panminxiang@Mac ~ % /usr/libexec/java_home -V
Matching Java Virtual Machines (4):
21.0.3 (arm64) "Oracle Corporation" - "Java SE 21.0.3" /Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home
17.0.11 (arm64) "Oracle Corporation" - "Java SE 17.0.11" /Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home
1.8.411.09 (arm64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
1.8.0_411 (arm64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk-1.8.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home
配置环境变量
~/.zshrc 文件内容
# 感谢 /usr/libexec/java_home 工具,能让我轻松获不同 jdk 的 JAVA_HOME
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8.0)
export JAVA_17_HOME=$(/usr/libexec/java_home -v17)
export JAVA_21_HOME=$(/usr/libexec/java_home -v21)
# 默认 jdk8
export JAVA_HOME=$JAVA_8_HOME
export CLASS_PATH=$JAVA_HOME/lib
# 加入到 $PATH 中
export PATH=$PATH:$JAVA_HOME/bin
# 命令别名,轻松切换 jdk
alias jdk8="export JAVA_HOME=$JAVA_8_HOME"
alias jdk17="export JAVA_HOME=$JAVA_17_HOME"
alias jdk21="export JAVA_HOME=$JAVA_21_HOME"
标签:Java,jdk,panminxiang,Library,Mac,java,安装
From: https://www.cnblogs.com/bibiafa/p/18287929