很早以前Mac一直装的OracleJDK
,最近265的小版本更新了,又安装了。突然想到应该切换到OpenJDK上来,就试了试。其实没啥难的,可能有一些工具,比如:jmc
没有带上,只能怪它太肥了,如果要用的话,需要自己单独去下载,Oracle
也开源了它。
删除OracleJDK
删除/Library/Java/JavaVirtualMachines/
目录下的旧有JDK,顺便清除下面目录中的内容:
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin
/Library/PreferencePanes/JavaControlPanel.prefPane
~/Library/Application Support/Oracle/Java
这样基本清理了旧有JDK的内容,开始准备安装OpenJDK。
使用brew安装AdoptOpenJDK
使用homebrew安装AdoptOpenJDK,brew tap AdoptOpenJDK/openjdk
。如果接下来直接安装,会很慢,因为它会访问amazon美国的S3。下面会安装JDK8和JDK11,需要修改以下两个文件。进入如下目录/usr/local/Homebrew/Library/Taps/adoptopenjdk/homebrew-openjdk/Casks
。
修改adoptopenjdk8.rb
,修改文件中的url为:
# url "https://github.com/AdoptOpenJDK/openjdk8-binaries/releases/download/jdk8u265-b01/OpenJDK8U-jdk_x64_mac_hotspot_8u265b01.pkg"
url "https://mirrors4.tuna.tsinghua.edu.cn/AdoptOpenJDK/8/jdk/x64/mac/OpenJDK#{version.before_comma}U-jdk_x64_mac_hotspot_#{version.before_comma}u#{version.after_comma.before_colon}#{version.after_comma.after_colon}.pkg"
修改adoptopenjdk11.rb
,修改文件中的url为:
# url 'https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.8%2B10/OpenJDK11U-jdk_x64_mac_hotspot_11.0.8_10.pkg'
url 'https://mirrors4.tuna.tsinghua.edu.cn/AdoptOpenJDK/11/jdk/x64/mac/OpenJDK11U-jdk_x64_mac_hotspot_11.0.8_10.pkg'
可以看到这个ruby脚本实际就是按照url去下载,而我们将其定位到清华大学的服务器上,可以记一下这个网站:https://mirrors4.tuna.tsinghua.edu.cn/AdoptOpenJDK/
,后续版本更新了可以从这里找。但是为了能够让ruby脚本正常更新,我们安装完jdk后,revert一下刚才的改动。
brew cask install adoptopenjdk8
brew cask install adoptopenjdk11
分别安装完jdk后,回滚修改。
% git status
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: adoptopenjdk11.rb
modified: adoptopenjdk8.rb
no changes added to commit (use "git add" and/or "git commit -a")
% git checkout -- .
% git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
支持8和11的切换
修改~/.bash_profile
,相关内容如下:
export JAVA_8_HOME="/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home"
export JAVA_11_HOME="/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home"
alias jdk8='export JAVA_HOME=$JAVA_8_HOME'
alias jdk11='export JAVA_HOME=$JAVA_11_HOME'
export JAVA_HOME=$JAVA_8_HOME
运行source ~/.bash_profile
,可以通过jdk8
和jdk11
进行版本切换。
% java -version
openjdk version "1.8.0_265"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_265-b01)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.265-b01, mixed mode)
% jdk11
% java -version
openjdk version "11.0.8" 2020-07-14
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.8+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.8+10, mixed mode)
% jdk8
% java -version
openjdk version "1.8.0_265"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_265-b01)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.265-b01, mixed mode)
网友评论