How to set or change the default Java (JDK) version on OS X?

后端 未结 28 2305
渐次进展
渐次进展 2020-11-22 15:55

How can you change the default version of Java on a mac?

相关标签:
28条回答
  • 2020-11-22 16:17

    This tool will do the work for you:

    http://www.guigarage.com/2013/02/change-java-version-on-mac-os/

    It's a simple JavaOne that can be used to define the current Java Version. The version can be used in a shell that is opened after a version was selected in the tool.

    0 讨论(0)
  • 2020-11-22 16:21

    Previously I used alias'es in .zshrc for easy switching between versions but today I use SDKMAN. SDKMAN can also handle setting default java for the system, and downloading and installing new java versions.

    Once sdkman is installed you can then do commands similar to what is possible with the nvm tool for handling node versions.

    sdk list java will list the java versions available on your system as well as available online for installation including their identifier that you can use in the sdk use, sdk default and sdk install commands.

    e.g. to install Amazon Corretto 11.0.8 and ask if it should be the new default do this: sdk install java 11.0.8-amzn

    A feature I also use regularly is the .sdkmanrc file. If you place that in a directory on your machine and run the sdk env command in the directory then you can configure tool versions used only in that directory. It is also possible to make sdkman switch tool versions automatically using the sdkman_auto_env=true configuration.

    sdkman also supports handling other tools for the JVM such as gradle, kotlin, maven and more.

    For more information check out https://sdkman.io/usage#env

    0 讨论(0)
  • 2020-11-22 16:23

    This answer is an attempt to address: how to control java version system-wide (not just in currently running shell) when several versions of JDK are installed for development purposes on macOS El Capitan or newer (Sierra, High Sierra, Mojave). As far as I can tell, none of the current answers do that (*).

    As a developer, I use several JDKs, and I want to switch from one to the other easily. Usually I have the latest stable one for general use, and others for tests. But I don't want the system (e.g. when I start my IDE) to use the latest "early access" version I have for now. I want to control system's default, and that should be latest stable.

    The following approach works with Java 7 to 12 at least (early access at the time of this writing), with Oracle JDK or OpenJDK (including builds by AdoptOpenJDK produced after mid-October 2018).

    Solution without 3rd party tools:

    • leave all JDKs at their default location, under /Library/Java/JavaVirtualMachines. The system will pick the highest version by default.
    • To exclude a JDK from being picked by default, rename its Contents/Info.plist to Info.plist.disabled. That JDK can still be used when $JAVA_HOME points to it, or explicitly referenced in a script or configuration. It will simply be ignored by system's java command.

    System launcher will use the JDK with highest version among those that have an Info.plist file.

    When working in a shell with alternate JDK, pick your method among existing answers (jenv, or custom aliases/scripts around /usr/libexec/java_home, etc).


    Details of investigation in this gist.


    (*) Current answers are either obsolete (no longer valid for macOS El Capitan or Sierra), or only address a single JDK, or do not address the system-wide aspect. Many explain how to change $JAVA_HOME, but this only affects the current shell and what is launched from there. It won't affect an application started from OS launcher (unless you change the right file and logout/login, which is tedious). Same for jenv, it's cool and all, but as far as I can tell it merely changes environment variables, so it has the same limitation.

    0 讨论(0)
  • 2020-11-22 16:24

    Adding to the above answers, I put the following lines in my .bash_profile (or .zshrc for MacOS 10.15+) which makes it really convenient to switch (including @elektromin's comment for java 9):

    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"
    

    After inserting, execute $ source .bash_profile

    I can switch to Java 8 by typing the following:

    $ j8
    java version "1.8.0_102"
    Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
    Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)
    
    0 讨论(0)
  • 2020-11-22 16:24

    A small fish function based on /usr/libexec/java_home

    function jhome
        set JAVA_HOME (/usr/libexec/java_home $argv)
        echo "JAVA_HOME:" $JAVA_HOME
        echo "java -version:"
        java -version
    end
    

    If you don't use fish, you can do something similar in bash:

    #!/bin/bash
    
    jhome () {
      export JAVA_HOME=`/usr/libexec/java_home $@`
      echo "JAVA_HOME:" $JAVA_HOME
      echo "java -version:"
      java -version
    }
    

    Then to switch between javas do:

    $> jhome           #switches to latest java
    $> jhome -v 1.7    #switches to java 1.7
    $> jhome -v 1.6    #switches to java 1.6
    

    ref: https://gist.github.com/kenglxn/1843d552dff4d4233271

    0 讨论(0)
  • 2020-11-22 16:24

    add following command to the ~/.zshenv file

    export JAVA_HOME=`/usr/libexec/java_home -v 1.8` 
    
    0 讨论(0)
提交回复
热议问题