英文:
Can't set JAVA_HOME dynamically on macOS Catalina
问题
tl,dr;
执行一次 export JAVA_HOME=$(/usr/libexec/java_home -v 14)
不会改变已经设置的 JAVA_HOME
,无论之前的版本如何。
步骤
- 首先设置
JAVA_HOME
(例如使用export JAVA_HOME=$(/usr/libexec/java_home -v 11)
):
$ echo ${JAVA_HOME}
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
$ java -version
openjdk version "11.0.8" 2020-07-14
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.8+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.8+10, mixed mode)
- 尝试将其设置为不同的版本(例如
export JAVA_HOME=$(/usr/libexec/java_home -v 14)
)- 它仍然使用之前设置的java
(即使更新了JAVA_HOME
):
$ echo ${JAVA_HOME}
/Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home
$ java -version
openjdk version "11.0.8" 2020-07-14
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.8+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.8+10, mixed mode)
我不确定为什么现在不起作用 - 几个月前还好好的(大约2-3个月前)。
似乎有一个类似的主题(https://stackoverflow.com/questions/60012746/cant-set-java-home-on-catalina),但问题归结为不同的 shell。在我的情况下,我使用的是相同的 shell - 来自 brew 的 bash:
$ bash -version
GNU bash, version 5.0.18(1)-release (x86_64-apple-darwin19.5.0)
版权所有 (C) 2019 自由软件基金会(Free Software Foundation, Inc.)
许可证 GPLv3+:GNU GPL 第3版或更高版本 <http://gnu.org/licenses/gpl.html>
这是自由软件,您可以自由更改和重新发布它。
根据法律规定,不提供任何保证。
英文:
tl,dr;
Doing export JAVA_HOME=$(/usr/libexec/java_home -v 14)
once JAVA_HOME
was already set won't change active java
Steps
- set
JAVA_HOME
first (for example withexport JAVA_HOME=$(/usr/libexec/java_home -v 11)
):
$ echo ${JAVA_HOME}
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
$ java -version
openjdk version "11.0.8" 2020-07-14
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.8+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.8+10, mixed mode)
- Try to set it to different version (for example
export JAVA_HOME=$(/usr/libexec/java_home -v 14)
) - it still uses previously setjava
(even thoughJAVA_HOME
was updated):
$ echo ${JAVA_HOME}
/Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home
$ java -version
openjdk version "11.0.8" 2020-07-14
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.8+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.8+10, mixed mode)
I'm not sure why it's not working now - it was working just fine a while back (I'd say 2-3 months ago)
There is a seemingly similar topic (https://stackoverflow.com/questions/60012746/cant-set-java-home-on-catalina) but it boils down to different shell. In my case I'm using same shell - bash from brew:
$ bash -version
GNU bash, version 5.0.18(1)-release (x86_64-apple-darwin19.5.0)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
答案1
得分: 0
似乎更新 JAVA_HOME
并未在 PATH
变量中得到反映。
因此,请同时更新 PATH 变量,并将更新后的 JAVA_HOME
设置为导出的 PATH 命令中的第一个元素。
英文:
Seems update JAVA_HOME
is not getting reflected in PATH
variable.
So, please update PATH variable also and keep the updated JAVA_HOME
as the first element in the export PATH
command
答案2
得分: 0
好的,以下是您提供的内容的翻译:
我终于成功解决了这个问题。正如其他人指出的,一切都与PATH
变量有关。而且我确实在其中加入了JAVA_HOME/bin
……这导致了问题。今天在更新过程中,我注意到openjdk
brew 公式有这样的注释:
keg_only "it shadows the macOS `java` wrapper"
然后一切都“恍然大悟”。在稍微搜索后,我找到了 https://stackoverflow.com/questions/61646049/why-isnt-java-wrapper-not-picking-up-the-right-version-specified-in-java-home-o 这个链接,证实了这个问题。通过在我的.profile
文件中将JAVA_HOME
加入PATH
,我破坏了 macOS 的本地功能,它的java
包装器会基于当前设置的JAVA_HOME
选择活动的java
版本。
以下是我的示例会话(从打开shell并在.profile
文件中加入export JAVA_HOME=$(/usr/libexec/java_home -v 11)
)-- 我先使用配置文件中的默认设置,然后显式设置JAVA_HOME
,它会即时切换java
版本:
$ java -version
openjdk version "11.0.8" 2020-07-14
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.8+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.8+10, mixed mode)
$ export JAVA_HOME=$(/usr/libexec/java_home -v 14)
$ java -version
openjdk version "14.0.2" 2020-07-14
OpenJDK Runtime Environment AdoptOpenJDK (build 14.0.2+12)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 14.0.2+12, mixed mode, sharing)
英文:
OK, I finally managed to solve the issue. As other have pointed out, everything has to do with PATH
variable. And I indeed had JAVA_HOME/bin
in there… which causes the problem. Today during an update I notice that openjdk
brew formula had this comment:
keg_only "it shadows the macOS `java` wrapper"
And things immediately "clicked". After searching a bit I found https://stackoverflow.com/questions/61646049/why-isnt-java-wrapper-not-picking-up-the-right-version-specified-in-java-home-o which confirmed the issue. By including JAVA_HOME
in the PATH
in my .profile
file I was breaking native macOS functionality of it's java
wrapper, which selects active java
version based on currently set JAVA_HOME
.
This is my example session (from opening the shell and having export JAVA_HOME=$(/usr/libexec/java_home -v 11)
in the .profile
file) -- I have default from the profile, then I set explicitly JAVA_HOME
and it switches java
version on the fly:
$ java -version
openjdk version "11.0.8" 2020-07-14
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.8+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.8+10, mixed mode)
$ export JAVA_HOME=$(/usr/libexec/java_home -v 14)
$ java -version
openjdk version "14.0.2" 2020-07-14
OpenJDK Runtime Environment AdoptOpenJDK (build 14.0.2+12)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 14.0.2+12, mixed mode, sharing)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论