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

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

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

28条回答
  •  执念已碎
    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)
    

提交回复
热议问题