若是同時安裝了 adoptopenjdk 11 和 adoptopenjdk 8,同時但願 8 作爲默認 JDK,只須要把 /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Info.plist
裏的JVMVersion
的值由1.8.0_222
改成 x1.8.0_222
(大概第42行)。這樣咱們的MacOS默認JDK就成爲adoptopenjdk-8了。java
macOS下 JDK 默認安裝在 /Library/Java/JavaVirtualMachines
目錄下,同時提供了一個小工具/usr/libexec/java_home
幫助咱們快速的查看 JDK 相關的信息。
默認狀況下 MacOS 會自動的選擇 /Library/Java/JavaVirtualMachines
目錄下版本號最高的 JDK 作爲默認 JDK 。vim
➜ ~ java -version openjdk version "1.8.0_222" OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_222-b10) OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.222-b10, mixed mode)
➜ ~ /usr/libexec/java_home /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
查看全部bash
➜ ~ /usr/libexec/java_home -V Matching Java Virtual Machines (3): 1.8.0_222, x86_64: "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home 1.8.0_201, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home 1.7.0_80, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home
查看指定版本app
能夠經過/usr/libexec/java_home -v <version>
來過濾版本號。 返回前綴匹配到的最新 JDK。工具
➜ ~ /usr/libexec/java_home -v 1 /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home ➜ ~ /usr/libexec/java_home -v 1.7 /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home ➜ ~ /usr/libexec/java_home -v 1.8.0_201 /Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home ➜ ~ /usr/libexec/java_home -v 1.8 /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
能夠經過java_home
的exec
選項來執行單次任務。
/usr/libexec/java_home -v version --exec command測試
➜ ~ /usr/libexec/java_home -v 1.7 --exec java -version java version "1.7.0_80" Java(TM) SE Runtime Environment (build 1.7.0_80-b15) Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
Shell 環境只須要指定一下JAVA_HOME
環境變量就能夠。ui
➜ ~ export JAVA_HOME=`/usr/libexec/java_home -v 1.7` ➜ ~ java -version java version "1.7.0_80" Java(TM) SE Runtime Environment (build 1.7.0_80-b15) Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
爲了方便切換把如下別名配置粘到對應的 Shell 的配置文件 .bashrc 或 .zshrc,而後就能夠方便的切換 JDK 版本了。code
別名配置
須要根據本身實際已安裝 JDK 作增減。xml
alias j12="export JAVA_HOME=`/usr/libexec/java_home -v 12`; java -version" alias j11="export JAVA_HOME=`/usr/libexec/java_home -v 11`; java -version" alias j10="export JAVA_HOME=`/usr/libexec/java_home -v 10`; java -version" alias j9="export JAVA_HOME=`/usr/libexec/java_home -v 9`; java -version" alias j8="export JAVA_HOME=`/usr/libexec/java_home -v 1.8`; java -version" alias j7="export JAVA_HOME=`/usr/libexec/java_home -v 1.7`; java -version"
使用效果排序
➜ ~ j7 java version "1.7.0_80" Java(TM) SE Runtime Environment (build 1.7.0_80-b15) Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode) ➜ ~ j8 java version "1.8.0_201" Java(TM) SE Runtime Environment (build 1.8.0_201-b09) Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode) ➜ ~ j11 openjdk version "11.0.4" 2019-07-16 OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.4+11) OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.4+11, mixed mode)
GUI 程序使用的默認 Java 也是 /usr/libexec/java_home -V
中看到的最高版本。
建立setenv.javahome.plist
並經過launchctl
指定設置環境變量JAVA_HOME
,須要註銷再登陸才生效。並且有些程序不兼容該方式。使用該方式以後,/usr/libexec/java_home
的顯示跟實際執行也會出現不一致。
生成 setenv.javahome.plist
並加載的腳本:
cat > ~/Library/LaunchAgents/setenv.javahome.plist <<EOF <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>setenv.javahome</string> <key>ProgramArguments</key> <array> <string>/bin/launchctl</string> <string>setenv</string> <string>JAVA_HOME</string> <string>/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home</string> </array> <key>RunAtLoad</key> <true/> <key>ServiceIPC</key> <false/> </dict> </plist> EOF launchctl load ~/Library/LaunchAgents/setenv.javahome.plist
咱們還能夠經過修改版本號實現指定版本的JDK作爲默認JDK, 我目前正在使用該方式。
/usr/libexec/java_home
是經過/Library/Java/JavaVirtualMachines/<JDK>/Contents/Info.plist
裏的JVMVersion
值來獲取版本號的,因此只須要修改這個值爲當前最大版本號便可實現指定默認 JDK。通過測試這個仍是即時生效。
像我安裝過adoptopenjdk 11 ,但仍是但願 adoptopenjdk 8作爲默認 JDK,只須要把 /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Info.plist
裏的JVMVersion
的值由1.8.0_222
改成 x1.8.0_222
(大概第42行)。這樣咱們的adoptopenjdk-8.jdk就變成最新版本的 JDK 了。
排序是經過 ASCII 值來排的,版本號只要改的比最新的 11 大都行,字符'x'的ASCII值遠大於字符'1', 爲了方便版本區分我只加了一個字符 x 。
修改完成以後再查看 JDK 信息,就會發現咱們修改的x1.8.0_222會排到第一位,同時 Java version 是 1.8。
➜ ~ /usr/libexec/java_home -V Matching Java Virtual Machines (4): x1.8.0_222, x86_64: "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home 11.0.4, x86_64: "AdoptOpenJDK 11" /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home 1.8.0_201, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home 1.7.0_80, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home ➜ ~ java -version openjdk version "1.8.0_222" OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_222-b10) OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.222-b10, mixed mode)
操做流程:
備份原始文件並打開,並使用 vim 編輯.
sudo cp /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Info.plist /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Info.plist.bak sudo vim /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Info.plist
修改後的Info.plist
文件差別
➜ ~ diff -c /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Info.plist /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Info.plist.bak *** /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Info.plist 2019-10-27 14:01:10.000000000 +0800 --- /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Info.plist.bak 2019-10-27 14:17:36.000000000 +0800 *************** *** 39,45 **** <key>JVMVendor</key> <string>AdoptOpenJDK</string> <key>JVMVersion</key> ! <string>x1.8.0_222</string> </dict> </dict> </plist> --- 39,45 ---- <key>JVMVendor</key> <string>AdoptOpenJDK</string> <key>JVMVersion</key> ! <string>1.8.0_222</string> </dict> </dict> </plist>
經過簡單的探索,就能發現咱們使用的 java
實際上是軟鏈到 /System/Library/Frameworks/JavaVM.framework/Versions/A/Commands/java
。
➜ ~ greadlink -f `which java` /System/Library/Frameworks/JavaVM.framework/Versions/A/Commands/java
greadlink 須要安裝coreutils
brew install coreutils
而 /System/Library/Frameworks/JavaVM.framework/Versions/A/Commands
目錄下的文件可能是固定 38k 大小。猜想這些文件應該只是包裝器,根據系統配置把命令轉發給相應的 JDK 的對應命令。
➜ ~ ll /System/Library/Frameworks/JavaVM.framework/Versions/A/Commands total 1200 -rwxr-xr-x 1 root wheel 38K Sep 30 04:28 appletviewer -rwxr-xr-x 1 root wheel 38K Sep 30 04:28 apt -rwxr-xr-x 1 root wheel 38K Sep 30 04:28 extcheck -rwxr-xr-x 1 root wheel 38K Sep 30 04:28 idlj -rwxr-xr-x 1 root wheel 38K Sep 30 04:28 jar -rwxr-xr-x 1 root wheel 38K Sep 30 04:28 jarsigner -rwxr-xr-x 1 root wheel 38K Sep 30 04:28 java -rwxr-xr-x 1 root wheel 47K Sep 30 04:28 java_home -rwxr-xr-x 1 root wheel 38K Sep 30 04:28 javac -rwxr-xr-x 1 root wheel 38K Sep 30 04:28 javadoc ...