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

后端 未结 10 796
暖寄归人
暖寄归人 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 15:06

    With Homebrew and jenv:

    Assumption: Mac machine and you already have installed homebrew.

    Install cask:

    $ brew tap caskroom/cask
    $ brew tap caskroom/versions
    

    To install latest java:

    $ brew cask install java
    

    To install java 8:

    $ brew cask install java8
    

    To install java 9:

    $ brew cask install java9
    

    If you want to install/manage multiple version then you can use 'jenv':

    Install and configure jenv:

    $ brew install jenv
    $ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
    $ echo 'eval "$(jenv init -)"' >> ~/.bash_profile
    $ source ~/.bash_profile
    

    Add the installed java to jenv:

    $ jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home
    $ jenv add /Library/Java/JavaVirtualMachines/jdk1.11.0_2.jdk/Contents/Home
    

    To see all the installed java:

    $ jenv versions
    

    Above command will give the list of installed java:

    * system (set by /Users/lyncean/.jenv/version)
    1.8
    1.8.0.202-ea
    oracle64-1.8.0.202-ea
    

    Configure the java version which you want to use:

    $ jenv global oracle64-1.6.0.39
    
    0 讨论(0)
  • 2020-12-22 15:07

    You can use asdf to install and switch between multiple java versions. It has plugins for other languages as well. You can install asdf with Homebrew

    brew install asdf
    

    When asdf is configured, install java plugin

    asdf plugin-add java
    

    Pick a version to install

    asdf list-all java
    

    For example to install and configure adoptopenjdk8

    asdf install java adoptopenjdk-8.0.272+10
    asdf global java adoptopenjdk-8.0.272+10
    

    And finally if needed, configure JAVA_HOME for your shell. Just add to your shell init script such as ~/.zshrc in case of zsh:

    . ~/.asdf/plugins/java/set-java-home.zsh
    
    0 讨论(0)
  • 2020-12-22 15:09

    IMHO, There is no need to install all the additional applications/packages.

    Check available versions using the command:

    > /usr/libexec/java_home -V
    Matching Java Virtual Machines (8):
        11, x86_64: "Java SE 11-ea" /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home
        10.0.2, x86_64: "Java SE 10.0.2"    /Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home
        9.0.1, x86_64:  "Java SE 9.0.1" /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home
        1.8.0_181-zulu-8.31.0.1, x86_64:    "Zulu 8"    /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home
        1.8.0_151, x86_64:  "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home
        1.7.0_80, x86_64:   "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home
        1.6.0_65-b14-468, x86_64:   "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
        1.6.0_65-b14-468, i386: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
    

    Now if you want to pick Azul JDK 8 in the above list, and NOT Oracle's Java SE 8, invoke the command as below:

    > /usr/libexec/java_home -v 1.8.0_181
    /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home
    

    To pick Oracle's Java SE 8 you would invoke the command:

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

    As you can see the version number provided shall be the unique set of strings: 1.8.0_181 vs 1.8.0_151

    0 讨论(0)
  • 2020-12-22 15:11

    This answer extends on Jayson's excellent answer with some more opinionated guidance on the best approach for your use case:

    • SDKMAN is the best solution for most users. It's easy to use, doesn't have any weird configuration, and makes managing multiple versions for lots of other Java ecosystem projects easy as well.
    • Downloading Java versions via Homebrew and switching versions via jenv is a good option, but requires more work. For example, the Homebrew commands in this highly upvoted answer don't work anymore. jenv is slightly harder to setup, the plugins aren't well documented, and the README says the project is looking for a new maintainer. jenv is still a great project, solves the job, and the community should be thankful for the wonderful contribution. SDKMAN is just the better option cause it's so great.
    • Jabba is written is a multi-platform solution that provides the same interface on Mac, Windows, and PC (it's written in Go and that's what allows it to be multiplatform). If you care about a multiplatform solution, this is a huge selling point. If you only care about running multiple versions on your Mac, then you don't need a multiplatform solution. SDKMAN's support for tens of popular SDKs is what you're missing out on if you go with Jabba.

    Managing versions manually is probably the worst option. If you decide to manually switch versions, you can use this Bash code instead of Jayson's verbose code (code snippet from the homebrew-openjdk README:

    jdk() {
            version=$1
            export JAVA_HOME=$(/usr/libexec/java_home -v"$version");
            java -version
     }
    

    Jayson's answer provides the basic commands for SDKMAN and jenv. Here's more info on SDKMAN and more info on jenv if you'd like more background on these tools.

    0 讨论(0)
提交回复
热议问题