要查看已安装的版本,请使用:
/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