问题描述
我使用的是Amazon corretto JDK,而较早的版本仅在Mac OS中安装了1.8,昨天我使用Mac OS软件包安装程序安装了Amazon corretto JDK 11,然后将默认的Java版本更改为11.
I am using Amazon corretto JDK and earlier had only 1.8 installed in my Mac OS, yesterday I installed Amazon corretto JDK 11 using the Mac OS package installer and after that it changed my default java version to 11.
我关注了受好评的 Jenv上的中级博客以便在Mac OS上管理多个版本,但我的默认Java版本仍未切换回1.8,并且在遵循stackoverflow答案或中等要求时也没有出现任何错误.
I followed highly rated medium blog on Jenv to manage multiple version on Mac OS but still my default java version is not switching back to 1.8 and didn't get any error while following stackoverflow answer or medium post.
jenv版本
system
1.8
* 1.8.0.252 (set by /Users/my user/.jenv/version) // note `*` that should tell the current version IMO
11
11.0
11.0.7
corretto64-1.8.0.252
corretto64-11.0.7
java -version
output of java -version
注意:我有多个使用不同JDK版本的应用程序,并且我不想使用别名方式来管理JDK版本,我对Jenv感兴趣,我可以在其中全局设置我的JDK版本(1.8中我的案例)和本地(基于特定应用程序使用的JDK).
推荐答案
jenv 在后台使用/Library/Java/JavaVirtualMachines目录.
Behind the scenes jenv uses the /Library/Java/JavaVirtualMachines directory.
然后,您还可以键入/usr/libexec/java_home -V
以显示所有可用的JDK并分析您的问题:
Then you could also type /usr/libexec/java_home -V
to show all the available JDKs and analyse your issue:
Matching Java Virtual Machines (4):
11.0.2_2-OracleJDK, x86_64: "Java SE 11.0.2" /Library/Java/JavaVirtualMachines/OracleJDK-jdk-11.0.2.jdk/Contents/Home
11.0.2_1-OpenJDK, x86_64: "OpenJDK 11.0.2" /Library/Java/JavaVirtualMachines/OpenJDK-jdk-11.0.2.jdk/Contents/Home
1.8.0_11, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home
1.7.0_45, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/OracleJDK-jdk-11.0.2.jdk/Contents/Home
在此示例中,export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
设置JDK 1.8版本.
In this exemple export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
set the JDK 1.8 version.
因此,您甚至可以定义以下别名:
Thus, you even could define the following aliases:
java11_OpenJDK_export='export JAVA_HOME=$(/usr/libexec/java_home -v 11.0.2_1-OpenJDK)'
java11_OracleJDK_export='export JAVA_HOME=$(/usr/libexec/java_home -v 11.0.2_2-OracleJDK)'
java7_export='export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)'
java8_export='export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)'
注意:在 Contents/Info.plist 文件部分中的每个JDK安装中定义了java_home -v
之后要使用的版本 JVMVersion .
NB: The version to use after java_home -v
is defined in each JDK installation in the Contents/Info.plist file section JVMVersion.
我进行了更改,以便在OpenJDK和Oracle JDK之间轻松切换:
I changed it, to easily switch between the OpenJDK and the Oracle JDK:
$ tail -5 /Library/Java/JavaVirtualMachines/OpenJDK-jdk-11.0.2.jdk/Contents/Info.plist | head -2
<key>JVMVersion</key>
<string>11.0.2_1-OpenJDK</string>
$ tail -5 /Library/Java/JavaVirtualMachines/OracleJDK-jdk-11.0.2.jdk/Contents/Info.plist | head -2
<key>JVMVersion</key>
<string>11.0.2_2-OracleJDK</string>
因此,根据您的情况,您有两个1.8版本(1.8和1.8.0.252).您很有可能应该更新其 Contents/Info.plist 文件部分 JVMVersion .
So, in your case as you have two 1.8 versions (1.8 and 1.8.0.252). It is more than likely that you should update their Contents/Info.plist file section JVMVersion.
这篇关于无法使用Jenv在Mac OS中更改JDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!