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

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

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

28条回答
  •  甜味超标
    2020-11-22 16:17

    Add the following line of code to your .zshrc (or bash_profile):

    alias j='f(){ export JAVA_HOME=`/usr/libexec/java_home -v $1` };f'
    

    Save to session:

    $ source .zshrc
    

    Run command (e.g. j 13, j14, j1.8...)

    j 1.8
    

    Explanation This is parameterised so you do not need to update the script like other solutions posted. If you do not have the JVM installed you are told. Sample cases below:

    /Users/user/IDE/project $ j 1.8
    /Users/user/IDE/project $ 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)
    /Users/user/IDE/project $ j 13
    /Users/user/IDE/project $ java -version
    openjdk version "13.0.2" 2020-01-14
    OpenJDK Runtime Environment (build 13.0.2+8)
    OpenJDK 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)
    /Users/user/IDE/project $ j 1.7
    Unable to find any JVMs matching version "1.7".
    

提交回复
热议问题