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

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

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

28条回答
  •  无人共我
    2020-11-22 16:01

    Here is how I do it on my Linux (Ubuntu / Mint mate), I guess Mac can do it similarly.


    Install & config

    Steps:

    • [Basic - part]
    • Download jdk (the .tgz file) by hand.
    • Uncompress & rename properly, at a proper location.
      e.g /mnt/star/program/java/jdk-1.8
    • Make a soft link, which will be changed to switch java version later.
      e.g ln -s /mnt/star/program/java/jdk-1.8 /mnt/star/program/java/java
      Thus /mnt/star/program/java/java is the soft link.
    • Set JAVA_HOME in a start script.
      Could use file like /etc/profile.d/eric.sh, or just use ~/.bashrc.
      e.g JAVA_HOME=/mnt/star/program/java/java
    • Then open a new bash shell. java -version should print the java version.
    • [More version - part]
    • Download & install more Java version, as need, similar as above steps.
      e.g
      /mnt/star/program/java/jdk-11
    • [Switch - part]
    • In ~/.bashrc, define variable for various Java version.
      e.g
      _E_JAVA_HOME_11='/mnt/star/program/java/jdk-11'
      _E_JAVA_HOME_8='/mnt/star/program/java/jdk-8'
      # dir of default version,
      _E_JAVA_HOME_D=$_E_JAVA_HOME_8
    • In ~/.bashrc, define command to switch Java version.
      e.g
      ## switch java version,
      alias jv11="rm $JAVA_HOME; ln -s $_E_JAVA_HOME_11 $JAVA_HOME"
      alias jv8="rm $JAVA_HOME; ln -s $_E_JAVA_HOME_8 $JAVA_HOME"
      # default java version,
      alias jvd="rm $JAVA_HOME; ln -s $_E_JAVA_HOME_D $JAVA_HOME"
      alias jv="java -version"
    • In terminal, source ~/.bashrc to make the changes take effect.
    • Then could switch using the defined commands.

    Commands - from above config

    Commands:

    • jv11
      Switch to Java 11
    • jv8
      Switch to Java 8
    • jvd
      Switch to default Java version, which is denoted by _E_JAVA_HOME_D defined above.
    • jv
      Show java version.

    Example output:

    eric@eric-pc:~$ jv
    java version "1.8.0_191"
    Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
    Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
    
    eric@eric-pc:~$ jv11
    eric@eric-pc:~$ jv
    java version "11.0.1" 2018-10-16 LTS
    Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
    Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)
    
    eric@eric-pc:~$ jvd
    eric@eric-pc:~$ jv
    java version "1.8.0_191"
    Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
    Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
    
    eric@eric-pc:~$ 
    

    Mechanism

    • It switch by changing the soft link, which is used as JAVA_HOME.

    Tips

    • On my machine when install jdk by hand, I keep the minor version, then make a soft link with the major version but without the minor version.
      e.g
      // this is the actual dir,
      jdk1.8.0_191

      // this is a soft link to jdk1.8.0_191
      jdk-8

      // this is a soft link to jdk-8 or jdk-11
      java

    • I define command alias in ~/.bashrc, but define variable in a separate file.
      I am using ~/.eric_var to define the variables, and ~/.bashrc will source it (e.g source $HOME/.eric_var).

提交回复
热议问题