How do I install Java on Mac OSX allowing version switching?

后端 未结 10 831
暖寄归人
暖寄归人 2020-12-22 14:37

I want to install OpenJDK Java on Mac OSX and have it work alongside other JDK\'s since it is a newer release. Currently, I downloaded the tar.gz and placed it in my path b

10条回答
  •  北海茫月
    2020-12-22 14:54

    Manually switching system-default version without 3rd party tools:

    As detailed in this older answer, on macOS /usr/bin/java is a wrapper tool that will use Java version pointed by JAVA_HOME or if that variable is not set will look for Java installations under /Library/Java/JavaVirtualMachines/ and will use the one with highest version. It determines versions by looking at Contents/Info.plist under each package.

    Armed with this knowledge you can:

    • control which version the system will use by renaming Info.plist in versions you don't want to use as default (that file is not used by the actual Java runtime itself).
    • control which version to use for specific tasks by setting $JAVA_HOME

    I've just verified this is still true with OpenJDK & Mojave.

    On a brand new system, there is no Java version installed:

    $ java -version
    No Java runtime present, requesting install.
    

    Cancel this, download OpenJDK 11 & 12ea on https://jdk.java.net ; install OpenJDK11:

    $ cd /Library/Java/JavaVirtualMachines/
    $ sudo tar xzf ~/Downloads/openjdk-11.0.1_osx-x64_bin.tar.gz
    

    System java is now 11:

    $ java -version
    openjdk version "11.0.1" 2018-10-16
    [...]
    

    Install OpenJDK12 (early access at the moment):

    $ sudo tar xzf ~/Downloads/openjdk-12-ea+17_osx-x64_bin.tar.gz 
    

    System java is now 12:

    $ java -version
    openjdk version "12-ea" 2019-03-19
    [...]
    

    Now let's "hide" OpenJDK 12 from system java wrapper:

    $ cd jdk-12.jdk/Contents/
    $ sudo mv Info.plist Info.plist.disabled
    

    System java is back to 11:

    $ java -version
    openjdk version "11.0.1" 2018-10-16
    [...]
    

    And you can still use version 12 punctually by manually setting JAVA_HOME:

    $ export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-12.jdk/Contents/Home
    $ java -version
    openjdk version "12-ea" 2019-03-19
    [...]
    

提交回复
热议问题