无法在 macOS Catalina 上动态设置 JAVA_HOME。

huangapple go评论91阅读模式
英文:

Can't set JAVA_HOME dynamically on macOS Catalina

问题

tl,dr;
执行一次 export JAVA_HOME=$(/usr/libexec/java_home -v 14) 不会改变已经设置的 JAVA_HOME,无论之前的版本如何。


步骤

  1. 首先设置 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)
  1. 尝试将其设置为不同的版本(例如 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

  1. set JAVA_HOME first (for example with export JAVA_HOME=$(/usr/libexec/java_home -v 11)):
$ echo ${JAVA_HOME}
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home

$ java -version
openjdk version &quot;11.0.8&quot; 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)
  1. Try to set it to different version (for example export JAVA_HOME=$(/usr/libexec/java_home -v 14)) - it still uses previously set java (even though JAVA_HOME was updated):
$ echo ${JAVA_HOME}
/Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home

$ java -version
openjdk version &quot;11.0.8&quot; 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 &lt;http://gnu.org/licenses/gpl.html&gt;

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 &quot;it shadows the macOS `java` wrapper&quot;

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 &quot;11.0.8&quot; 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 &quot;14.0.2&quot; 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)

huangapple
  • 本文由 发表于 2020年7月23日 21:48:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63055841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定