下载地址:https://jdk.java.net/archive/

安装

  1. Ξ Contents/Home brew install openjdk@11
  2. ==> Downloading https://ghcr.io/v2/homebrew/core/openjdk/11/manifests/11.0.15-1
  3. Already downloaded: /Users/twx/Library/Caches/Homebrew/downloads/4c40f869070c4b174f1e32b737ce0798120d815667d2825c9d49cd8db4294472--openjdk@11-11.0.15-1.bottle_manifest.json
  4. ==> Downloading https://ghcr.io/v2/homebrew/core/openjdk/11/blobs/sha256:773983fd85afacf11590381ae697d37dbf96ca9ecaf4b208354a01c62be409b7
  5. ==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:773983fd85afacf11590381ae697d37dbf96ca9ecaf4b208354a01c62be409b7?se=2022-06-27T01%3A
  6. ######################################################################## 100.0%
  7. ==> Pouring openjdk@11--11.0.15.monterey.bottle.1.tar.gz
  8. ==> Caveats
  9. For the system Java wrappers to find this JDK, symlink it with
  10. sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk
  11. openjdk@11 is keg-only, which means it was not symlinked into /usr/local,
  12. because this is an alternate version of another formula.
  13. If you need to have openjdk@11 first in your PATH, run:
  14. echo 'export PATH="/usr/local/opt/openjdk@11/bin:$PATH"' >> ~/.zshrc
  15. For compilers to find openjdk@11 you may need to set:
  16. export CPPFLAGS="-I/usr/local/opt/openjdk@11/include"


切换版本

/usr/libexec/java_home: This will show you where the current JDK home is

$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/openjdk-14.0.1/Contents/Home

/usr/libexec/java_home -V: This lists all installed JDKs, which is shown below:

$ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
    14.0.1, x86_64:    "OpenJDK 14.0.1"    /Library/Java/JavaVirtualMachines/openjdk-14.0.1/Contents/Home
    1.8.0_261, x86_64:    "Java SE 8"    /Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/openjdk-14.0.1/Contents/Home

To switch between JDKs, use /usr/libexec/java_home -v version

$ /usr/libexec/java_home -v 1.8.0_261
/Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home

切换:

alias jdk8="export JAVA_HOME=$JAVA_8_HOME"
alias jdk11="export JAVA_HOME=$JAVA_11_HOME"
alias jdk12="export JAVA_HOME=$JAVA_12_HOME"
alias jdk14="export JAVA_HOME=$JAVA_14_HOME"

export JAVA_8_HOME=$(/usr/libexec/java_home -v 1.8.0_261)
export JAVA_11_HOME=$(/usr/libexec/java_home -v 11.0.9)
export JAVA_12_HOME=$(/usr/libexec/java_home -v 12.0.2)
export JAVA_14_HOME=$(/usr/libexec/java_home -v 14.0.1)
#export JAVA_8_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home
#export JAVA_HOME=$JAVA_8_HOME

export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$HADOOP_HOME/bin:$PATH
# default to Java 15
java15