简体   繁体   English

/usr/libexec/java_home -v<version> 找到正确的 JDK 版本,但仅在第一次更新</version>

[英]/usr/libexec/java_home -v <version> finds the right JDK version but updates it only the first time

I have just got a mac mini with M1 processor, and I find this behaviour very weird.我刚买了一台带有 M1 处理器的 mac mini,我觉得这种行为很奇怪。 Basically, when running /usr/libexec/java_home -v ## I can see that only the first time the JDK path is found and replaced.基本上,在运行/usr/libexec/java_home -v ##时,我可以看到只有第一次找到并替换了 JDK 路径。 If I run it a second time even with a -V flag, I see it finds the right JDK path, yet it never replaces the old one, in fact making the whole thing stale.如果我使用-V标志再次运行它,我会发现它找到了正确的 JDK 路径,但它永远不会替换旧路径,实际上使整个事情变得陈旧。

Any idea why?知道为什么吗? This is an example taken from my terminal.这是从我的终端中获取的示例。

lda@Lucios-Mac-mini ~ % /usr/libexec/java_home -V   
Matching Java Virtual Machines (3):
    15 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 15" /Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home
    13.0.5.1 (arm64) "Azul Systems, Inc." - "Zulu 13.35.1017" /Library/Java/JavaVirtualMachines/zulu-13.jdk/Contents/Home
    11.0.9.1 (arm64) "Azul Systems, Inc." - "Zulu 11.43.1015" /Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home

lda@Lucios-Mac-mini ~ % java --version              
openjdk 15 2020-09-15
OpenJDK Runtime Environment AdoptOpenJDK (build 15+36)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 15+36, mixed mode, sharing)

lda@Lucios-Mac-mini ~ % export JAVA_HOME=$(/usr/libexec/java_home -v11) 

lda@Lucios-Mac-mini ~ % java --version                                 
openjdk 11.0.9.1 2020-11-04 LTS
OpenJDK Runtime Environment Zulu11.43+1015-CA (build 11.0.9.1+1-LTS)
OpenJDK 64-Bit Server VM Zulu11.43+1015-CA (build 11.0.9.1+1-LTS, mixed mode)

lda@Lucios-Mac-mini ~ % export JAVA_HOME=$(/usr/libexec/java_home -v15)

lda@Lucios-Mac-mini ~ % java --version                                 
openjdk 11.0.9.1 2020-11-04 LTS
OpenJDK Runtime Environment Zulu11.43+1015-CA (build 11.0.9.1+1-LTS)
OpenJDK 64-Bit Server VM Zulu11.43+1015-CA (build 11.0.9.1+1-LTS, mixed mode)

lda@Lucios-Mac-mini ~ % export JAVA_HOME=$(/usr/libexec/java_home -v13)

lda@Lucios-Mac-mini ~ % java --version                                 
openjdk 11.0.9.1 2020-11-04 LTS
OpenJDK Runtime Environment Zulu11.43+1015-CA (build 11.0.9.1+1-LTS)
OpenJDK 64-Bit Server VM Zulu11.43+1015-CA (build 11.0.9.1+1-LTS, mixed mode)

I've got an M1 mac, and for me the error is definitely with java_home always returning the same value, not with an inability to reassign shell variables (ie JAVA_HOME).我有一个 M1 mac,对我来说,错误肯定是 java_home 总是返回相同的值,而不是无法重新分配 shell 变量(即 JAVA_HOME)。

I've got an M1 mac and I have installed azul jvms:我有一个 M1 mac 并且我已经安装了 azul jvms:

$ /usr/libexec/java_home -V
    Matching Java Virtual Machines (3):
    11.0.9.1 (arm64) "Azul Systems, Inc." - "Zulu 11.43.1021" /Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home
    9.0.4.0.11 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
    1.8.0_275 (arm64) "Azul Systems, Inc." - "Zulu 8.50.0.1017" /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home

and I get this always zulu-11:我总是得到这个zulu-11:

$ /usr/libexec/java_home -v 1.8
/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home

and still get zulu-11 for:并且仍然获得zulu-11:

$ /usr/libexec/java_home -v9
/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home

I thought I could work-around by filtering stdout from just calling with the '-V' argument, but the results of that call go to stderr.我想我可以通过过滤标准输出来解决这个问题,只是使用'-V'参数调用,但是调用 go 到标准错误的结果。 So here's a version that works to select JDK 1.8 (specify the version just after 'awk'):所以这是一个适用于 select JDK 1.8 的版本(在 'awk' 之后指定版本):

$ export JAVA_HOME=`/usr/libexec/java_home -V  2>&1 >/dev/null | awk '/1.8/ {print $NF}'`

With thanks to first answers here: How can I pipe stderr, and not stdout?感谢这里的第一个答案: 我如何 pipe 标准错误,而不是标准输出? and here: Printing the last column of a line in a file在这里: 打印文件中一行的最后一列

Having said all of the above, it all seems to go a bit wobbly, when using zsh (perhaps I'm changing too many things at once), as the output is slightly different, and it includes the current setting.说了以上所有,go 似乎有点不稳定,当使用 zsh 时(也许我一次改变了太多东西),因为 output 略有不同,它包括当前设置。 I can get this to work:我可以让它工作:

JAVA_HOME=`/usr/libexec/java_home -V 2> /dev/fd/1 | awk '/^\ \ \ \ 1.8/ {print $NF}'`; java -version"

but all bets are off, if JAVA_HOME becomes unset, or wrong, as java_home will not print out the list in that situation.但是如果 JAVA_HOME 未设置或错误,所有赌注都将取消,因为 java_home 在这种情况下不会打印出列表。 Probably until java_home is fixed, I will stick with simple aliases in my.bash_profile/.zshrc of:可能在 java_home 修复之前,我会在 my.bash_profile/.zshrc 中使用简单的别名:

alias j8="export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home; java -version"
alias j11="export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home; java -version"

rather than:而不是:

alias j11="export JAVA_HOME=`/usr/libexec/java_home -V 2> /dev/fd/1 | awk '/^\ \ \ \ 11/ {print $NF}'`; java -version"
alias j8="export JAVA_HOME=`/usr/libexec/java_home -V 2> /dev/fd/1 | awk '/^\ \ \ \ 1.8/ {print $NF}'`; java -version"

which will work again, when java_home works, but TBH, I'm changing the jdk rarely enough for it to be a problem, and when I do... because azul go with a shorter directory name, which does not include patch version nos... I expect it will be OK.这将再次起作用,当 java_home 工作时,但 TBH,我很少更改 jdk 以至于它成为一个问题,当我这样做时......因为 azul go 具有较短的目录名称,不包括补丁版本号...我希望它会好起来的。 I'll cross that bridge, when I come to it.当我到达时,我会穿过那座桥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM