问题描述
我之前安装过 Java 16 并且正在使用它.现在我需要使用 Java 8.安装 Java 8 后,我意识到必须设置 JAVA_HOME 变量.我使用 macOS Big Sur 版本 11.5.2,我的终端使用 zsh shell.我关注了 youtube 上的一个视频,该视频告诉我执行以下步骤:
I had installed Java 16 before and was using it. Now I have a need to use Java 8. After installing Java 8 I realized that the JAVA_HOME variable has to be set. I use macOS Big Sur version 11.5.2 and my terminal uses zsh shell. I followed a video on youtube that told me to do the following steps:
- 触摸.zshrc
- 打开 -e .zshrc
- 在 .zshrc 文件中写下以下命令
- touch .zshrc
- open -e .zshrc
- inside .zshrc file write down the following commands
export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_301.jdk/Contents/Home'
export PATH = $JAVA_HOME/bin:$PATH
并保存文件
但是当我在终端上运行第 4 步时,我似乎收到以下消息:
but when I run the step 4 on my terminal I seem to get the following message:
/Library/Java/JavaVirtualMachines/jdk1.8.0_301.jdk/Contents/Home not found
但我可以保证目录结构是存在的.这是我运行时的输出ls/Library/Java/JavaVirtualMachines/jdk1.8.0_301.jdk/Contents/Home输出
But I can guarantee that the directory structure is present.Here is the output when I runls /Library/Java/JavaVirtualMachines/jdk1.8.0_301.jdk/Contents/Homeoutput
推荐答案
使用 /usr/libexec/java_home
命令.在我的机器上,我有:
Use the /usr/libexec/java_home
command. On my machine I have:
% /usr/libexec/java_home -V
Matching Java Virtual Machines (10):
16.0.1 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 16" /Library/Java/JavaVirtualMachines/adoptopenjdk-16.jdk/Contents/Home
16.0.1 (x86_64) "GraalVM Community" - "GraalVM CE 21.1.0" /Library/Java/JavaVirtualMachines/graalvm-ce-java16-21.1.0/Contents/Home
15.0.2 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 15" /Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home
14.0.2 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 14" /Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home
13.0.2 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 13" /Library/Java/JavaVirtualMachines/adoptopenjdk-13.jdk/Contents/Home
12.0.2 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 12" /Library/Java/JavaVirtualMachines/adoptopenjdk-12.jdk/Contents/Home
11.0.12 (x86_64) "GraalVM Community" - "GraalVM CE 21.2.0" /Library/Java/JavaVirtualMachines/graalvm-ce-java11-21.2.0/Contents/Home
11.0.11 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 11" /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
1.8.0_292 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
1.8.0_282+07 (x86_64) "GraalVM Community" - "GraalVM CE 21.0.0.2" /Library/Java/JavaVirtualMachines/graalvm-ce-java8-21.0.0.2/Contents/Home
/Library/Java/JavaVirtualMachines/adoptopenjdk-16.jdk/Contents/Home
使用以下命令获取 Java 1.8:
Use the following to get Java 1.8 home:
% /usr/libexec/java_home -v 1.8
/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
如果以上所有内容都适用于您的设置,请将以下内容添加到您的~/.zshrc
:
If all the above works for your set-up, add the following to your~/.zshrc
:
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
这篇关于在我的 Mac 上使用多个版本的 Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!