英文:
change java version in macOS BigSur
问题
我无法更改 macOS Big Sur 中的 Java 版本。
我有以下版本:
当我执行以下命令时:
/usr/libexec/java_home -V
我有以下版本:
匹配的 Java 虚拟机 (3):
11.0.8 (x86_64) "Oracle Corporation" - "Java SE 11.0.8" /Library/Java/JavaVirtualMachines/jdk-11.0.8.jdk/Contents/Home
1.8.261.12 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
1.8.0_261 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-11.0.8.jdk/Contents/Home
我想将默认的 Java 版本切换到 1.8,所以我执行以下命令:
export JAVA_HOME=/usr/libexec/java_home -v 1.8.0_261
然后当我检查 Java 版本时:
java -version
我仍然得到旧的版本(较高的版本):
java version "11.0.8" 2020-07-14 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.8+10-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.8+10-LTS, mixed mode)
为什么没有生效呢?
英文:
I can't change the java version in macOS BigSur.
i have the following versions :
when I execute this command
/usr/libexec/java_home -V
i have the following versions :
Matching Java Virtual Machines (3):
11.0.8 (x86_64) "Oracle Corporation" - "Java SE 11.0.8" /Library/Java/JavaVirtualMachines/jdk-11.0.8.jdk/Contents/Home
1.8.261.12 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
1.8.0_261 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-11.0.8.jdk/Contents/Home
i want to switch to java 1.8 as the default one , so I execute this command :
export JAVA_HOME=/usr/libexec/java_home -v 1.8.0_261
and then when I check java version :
java -version
I still got the old (higher one)
java version "11.0.8" 2020-07-14 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.8+10-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.8+10-LTS, mixed mode)
why it's not working?
答案1
得分: 17
请使用以下命令:
export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_261`
英文:
Please use below command :-
export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_261`
答案2
得分: 8
你需要取消设置 JAVA_HOME 并设置 JAVA_VERSION,然后它就会生效。
~% JAVA_VERSION=12 java -version
openjdk version "14.0.2" 2020-07-14
~% export JAVA_VERSION=12
~% java -version
openjdk version "14.0.2" 2020-07-14
~% unset JAVA_HOME
~% java -version
java version "12.0.1" 2019-04-16
~% JAVA_VERSION=13 java -version
java version "13-ea" 2019-09-17
或者,如果你需要设置 JAVA_HOME,在调用 /usr/libexec/java_home
之前需要取消设置:
function jav {
unset JAVA_HOME
export JAVA_HOME=`/usr/libexec/java_home -v $@`
}
~% jav 12
~% java -version
java version "12.0.1" 2019-04-16
~% jav 13
~% java -version
java version "13-ea" 2019-09-17
英文:
you have to unset JAVA_HOME and set JAVA_VERSION
then it works.
~% JAVA_VERSION=12 java -version
openjdk version "14.0.2" 2020-07-14
~% export JAVA_VERSION=12
~% java -version
openjdk version "14.0.2" 2020-07-14
~% unset JAVA_HOME
~% java -version
java version "12.0.1" 2019-04-16
~% JAVA_VERSION=13 java -version
java version "13-ea" 2019-09-17
or if you need to set JAVA_HOME you need to unset before calling /usr/libexec/java_home
function jav {
unset JAVA_HOME
export JAVA_HOME=`/usr/libexec/java_home -v $@`
}
~% jav 12
~% java -version
java version "12.0.1" 2019-04-16
~% jav 13
~% java -version
java version "13-ea" 2019-09-17
答案3
得分: 6
现在在 /usr/bin/java
处有新的二进制文件,并且它优先于 $JAVA_HOME/bin/java
。
英文:
Now there's new binary at /usr/bin/java
and it gets prioritized over $JAVA_HOME/bin/java
答案4
得分: 3
我在将 macOS 升级到 Big Sur 后,今天遇到了同样的问题而感到困惑。
我的 Mac 上有两个 JDK,Oracle JDK 11 和 Oracle JDK 8。
然后 /usr/libexec/java_home
总是返回 /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
,而忽略其他类似 -v 1.8
的参数。
经过一些测试,我发现,只有当您的 Mac 上有 Oracle JDK 1.8.X 版本时,才会出现这种情况。
因此,我删除了 Oracle JDK 1.8.x,/usr/libexec/java_home
可以返回正确的 JDK 11 路径。
然后我尝试了 AdoptOpenJDK 8,/usr/libexec/java_home
可以正确返回其路径,但会忽略 -v 11
参数,无法获取 JDK 11 的路径。
似乎 java_home
命令中确实存在一些问题,如果您的 Mac 上只有一个 JDK - 除了 Oracle JDK 8 之外 - 它应该能正常工作。如果您安装了多个 JDK,它可能只会返回最后安装的 JDK 路径。
因此,要么只保留一个 JDK 安装,要么使用绝对路径设置 $JAVA_HOME
环境变量都可以。
英文:
I have been confused with the same issue today after upgrade macOS to Big Sur.
There are two JDKs on my Mac, Oracle JDK 11 and Oracle JDK 8.
Then /usr/libexec/java_home
always return /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
and ignore other arguments like -v 1.8
After some tests I found that, this only happened when your Mac have an Oracle JDK 1.8.X version.
So I deleted the Oracle JDK 1.8.x, /usr/libexec/java_home
can return the correct JDK 11 path.
Then I tried AdoptOpenJDK 8, /usr/libexec/java_home
can return its path correctly, but ignore the -v 11
arg, can't get the path of JDK 11.
It seems there ARE some bugs in java_home
command, if you only have one JDK on your Mac - except Oracle JDK 8- it should works fine. If you have multiple
JDKs, maybe it just return the last installed JDK path.
So either only keep one JDK installation or set $JAVA_HOME
env with the absolute path will be OK.
答案5
得分: 3
我已经在我的.zshenv文件中添加了以下内容,并且它正常工作。谢谢munan。
## Java家目录
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
export JAVA_17_HOME=$(/usr/libexec/java_home -v17)
## Java别名
alias java8="export JAVA_HOME=$JAVA_8_HOME"
alias java11="export JAVA_HOME=$JAVA_11_HOME"
alias java17="export JAVA_HOME=$JAVA_17_HOME"
alias jv='java -version'
## 默认Java版本
java11
要从控制台更改Java版本,请使用:java8、java11或java17
英文:
I have added following on my .zshenv and it's working fine. Thanks munan.
## Java home
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
export JAVA_17_HOME=$(/usr/libexec/java_home -v17)
## Java alias
alias java8="export JAVA_HOME=$JAVA_8_HOME"
alias java11="export JAVA_HOME=$JAVA_11_HOME"
alias java17="export JAVA_HOME=$JAVA_17_HOME"
alias jv='java -version'
## Default Java
java11
To change the java version from console, use: java8, java11 or java17
答案6
得分: 3
在我看来,命令"/usr/libexec/java_home -v"
无法正常工作。我通过直接在文件.bash_profile
中导出Java路径来解决了这个问题:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home
然后执行命令:
source .bash_profile
之后,使用命令:
echo $JAVA_HOME
我发现JVM路径已正确设置。
英文:
in my opinion the command "/usr/libexec/java_home -v"
doesn't work. I solved exporting directly the java path in file .bash_profile:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home
and then doing the command:
source .bash_profile.
After this using the command:
echo $JAVA_HOME I've seen that the JVM path is set correctly.
答案7
得分: 1
更新:Mac OS 13.1
当您安装一个 openjdk 版本(除了系统/默认安装的版本之外),例如:
brew install openjdk@11
(在我这里,至少)它默认情况下没有链接到正确的位置。您需要按照这里的说明将其创建为符号链接:
sudo ln -sfn /opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk
这样,当您运行 /usr/libexec/java_home -V
时,您将看到所有已安装的版本:
匹配的 Java 虚拟机(2):
19.0.1 (arm64) “Homebrew” - “OpenJDK 19.0.1” /opt/homebrew/Cellar/openjdk/19.0.1/libexec/openjdk.jdk/Contents/Home
11.0.18 (arm64) “Homebrew” - “OpenJDK 11.0.18” /opt/homebrew/Cellar/openjdk@11/11.0.18/libexec/openjdk.jdk/Contents/Home
选择正确的版本只需设置环境变量 $JAVA_HOME
:
export JAVA_HOME=`/usr/libexec/java_home -v 11` # 设置为 Java 11,例如
运行 java --version
确认:
openjdk 11.0.18 2023-01-17
OpenJDK Runtime Environment Homebrew(构建 11.0.18+0)
OpenJDK 64-Bit Server VM Homebrew(构建 11.0.18+0,混合模式)
英文:
UPDATE: Mac OS 13.1
When you install an openjdk version (in addition to the system/default installed), for example:
brew install openjdk@11
(in my case, at least) it is not linked to the right location by default. You need to symlink it as outlined here
sudo ln -sfn /opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk
This way when you run /usr/libexec/java_home -V
, you will see all the installed versions
Matching Java Virtual Machines (2):
19.0.1 (arm64) "Homebrew" - "OpenJDK 19.0.1" /opt/homebrew/Cellar/openjdk/19.0.1/libexec/openjdk.jdk/Contents/Home
11.0.18 (arm64) "Homebrew" - "OpenJDK 11.0.18" /opt/homebrew/Cellar/openjdk@11/11.0.18/libexec/openjdk.jdk/Contents/Home
And choosing the right one is as simple as setting the env variable $JAVA_HOME
export JAVA_HOME=`/usr/libexec/java_home -v 11` # to set to java 11, for eg
Running java --version
confirms:
openjdk 11.0.18 2023-01-17
OpenJDK Runtime Environment Homebrew (build 11.0.18+0)
OpenJDK 64-Bit Server VM Homebrew (build 11.0.18+0, mixed mode)
答案8
得分: 0
我按照以下步骤来解决在 macOS BigSur 上设置所需的 Java 版本的问题:
当我执行以下命令时:
/usr/libexec/java_home -V
我得到了以下版本:
匹配的 Java 虚拟机(3):
17(x86_64)"Homebrew" - "OpenJDK 17" /usr/local/Cellar/openjdk/17/libexec/openjdk.jdk/Contents/Home
11.0.17.0-m1(x86_64)"IBM Corporation" - "IBM Semeru Runtime Open Edition 11" /Users/airasia/Library/Java/JavaVirtualMachines/semeru-11.0.17/Contents/Home
1.8.0_292(x86_64)"AdoptOpenJDK" - "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
/usr/local/Cellar/openjdk/17/libexec/openjdk.jdk/Contents/Home
然后,我想切换到 Java 1.8 作为默认版本,所以我执行了以下命令:
echo 'export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)' >> ~/.profile
然后当我检查 Java 版本时:
java --version
我仍然得到了旧的版本(较高的版本):
openjdk 17 2021-09-14
OpenJDK Runtime Environment Homebrew (build 17+0)
OpenJDK 64-Bit Server VM Homebrew (build 17+0, mixed mode, sharing)
~/.profile
文件的内容如下:
vim ~/.profile
export JAVA_HOME=/usr/local/opt/openjdk/bin
export PATH=$JAVA_HOME/bin:$PATH
然后我检查了环境变量:
set | grep JAVA
JAVA_HOME=JAVA_HOME=/usr/local/Cellar/openjdk/17/libexec/openjdk.jdk/Contents/Home
然后我在 ~/.profile
文件中添加了新的 JAVA_HOME
:
vim ~/.profile
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
export PATH=$JAVA_HOME/bin:$PATH
并执行了以下命令:
source ~/.profile
现在 Java 指向了 1.8.0_292
:
java -version
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_292-b10)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.292-b10, mixed mode)
要重新切换回 openjdk 17
,我只需要移除一行:
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
然后添加:
export JAVA_HOME=/usr/local/opt/openjdk/bin
并执行以下命令:
source ~/.profile
英文:
I followed following steps to fix an issue to set java version I needed in macOS BigSur. :
when I execute this command
/usr/libexec/java_home -V
I was having the following versions :
Matching Java Virtual Machines (3):
17 (x86_64) "Homebrew" - "OpenJDK 17" /usr/local/Cellar/openjdk/17/libexec/openjdk.jdk/Contents/Home
11.0.17.0-m1 (x86_64) "IBM Corporation" - "IBM Semeru Runtime Open Edition 11" /Users/airasia/Library/Java/JavaVirtualMachines/semeru-11.0.17/Contents/Home
1.8.0_292 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
/usr/local/Cellar/openjdk/17/libexec/openjdk.jdk/Contents/Home
Then, I wanted to switched to java 1.8 as the default one , so I executed this command :
echo 'export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)' >> ~/.profile
and then when I checked java version :
java --version
I was still getting the old (higher one)
openjdk 17 2021-09-14
OpenJDK Runtime Environment Homebrew (build 17+0)
OpenJDK 64-Bit Server VM Homebrew (build 17+0, mixed mode, sharing)
~/.profile
file was looking like,
vim ~/.profile
export JAVA_HOME=/usr/local/opt/openjdk/bin
export PATH=$JAVA_HOME/bin:$PATH
Then I checked
set | grep JAVA
JAVA_HOME=JAVA_HOME=/usr/local/Cellar/openjdk/17/libexec/openjdk.jdk/Contents/Home
Then I added new JAVA_HOME
in ~/.profile
file,
vim ~/.profile
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
export PATH=$JAVA_HOME/bin:$PATH
And executed it,
source ~/.profile
Now java pointing to 1.8.0_292
,
java -version
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_292-b10)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.292-b10, mixed mode)
To fallback to openjdk 17
again, I will just need to remove one line,
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
And add,
export JAVA_HOME=/usr/local/opt/openjdk/bin
And executed it,
source ~/.profile
答案9
得分: -1
我也是,太烦人了,他给我惹麻烦了。
他似乎通过设置$JAVA_VERSION
得到了解决。
请使用以下命令:
export JAVA_VERSION=11
英文:
Me too,So annoying, he caused me trouble.
He seems to be solved by set the '$JAVA_VERSION'.
Please use below command :-
export JAVA_VERSION=11
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论