要查看已装置的版本,请应用:
/usr/libexec/java_home -V
$ /usr/libexec/java_home -V
$ /usr/libexec/java_home -V
Matching Java Virtual Machines (4):
14.0.2 (x86_64) "Oracle Corporation" - "Java SE 14.0.2" /Library/Java/JavaVirtualMachines/jdk-14.0.2.jdk/Contents/Home
11.0.9 (x86_64) "GraalVM Community" - "GraalVM CE 20.3.0" /Library/Java/JavaVirtualMachines/graalvm-ce-java11-20.3.0/Contents/Home
1.8.271.09 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
1.8.0_265 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-14.0.2.jdk/Contents/Home
而后只需在您 ~/.bashrc
或~/.bash_profile
:
alias java8='export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.271.09)'
alias openjdk8='export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0_265)'
alias java11='export JAVA_HOME=$(/usr/libexec/java_home -v 11.0.9)'
alias java14='export JAVA_HOME=$(/usr/libexec/java_home -v 14.0.2)'
让脚本失效:
$ source ~/.bashrc
应用很简略,如
$ java11
就能够切换到 java11
如果不嫌麻烦,也能够装置一个专门工具,如 jenv