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

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

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

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

    If you have multiple versions and you want to run something by using a specific version, use this example:

    /usr/libexec/java_home -v 1.7.0_75 --exec java -jar you-file.jar
    
    0 讨论(0)
  • 2020-11-22 16:14

    From the Apple's official java_home(1) man page:

    **USAGE**
    
       /usr/libexec/java_home  helps  users  set a $JAVA_HOME in their login rc files, or provides a way for
       command-line Java tools to use the most appropriate JVM which can satisfy a minimum version or archi-
       tecture  requirement.  The --exec argument can invoke tools in the selected $JAVA_HOME/bin directory,
       which is useful for starting Java command-line tools from launchd plists without hardcoding the  full
       path to the Java command-line tool.
    
       Usage for bash-style shells:
              $ export JAVA_HOME=`/usr/libexec/java_home`
    
       Usage for csh-style shells:
              % setenv JAVA_HOME `/usr/libexec/java_home`
    
    0 讨论(0)
  • 2020-11-22 16:14

    JDK Switch Script

    I have adapted the answer from @Alex above and wrote the following to fix the code for Java 9.

    $ cat ~/.jdk
    #!/bin/bash
    
    #list available jdks
    alias jdks="/usr/libexec/java_home -V"
    # jdk version switching - e.g. `jdk 6` will switch to version 1.6
    function jdk() {
      echo "Switching java version $1";
    
      requestedVersion=$1
      oldStyleVersion=8
      # Set the version
      if [ $requestedVersion -gt $oldStyleVersion ]; then
        export JAVA_HOME=$(/usr/libexec/java_home -v $1);
      else
        export JAVA_HOME=`/usr/libexec/java_home -v 1.$1`;
      fi
    
      echo "Setting JAVA_HOME=$JAVA_HOME"
    
      which java
      java -version;
    }
    

    Switch to Java 8

    $ jdk 8
    Switching java version 8
    Setting JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
    /usr/bin/java
    java version "1.8.0_131"
    Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
    Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
    

    Switch to Java 9

    $ jdk 9
    Switching java version 9
    Setting JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home
    /usr/bin/java
    java version "9.0.1"
    Java(TM) SE Runtime Environment (build 9.0.1+11)
    Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)
    
    0 讨论(0)
  • 2020-11-22 16:14

    Based on @markhellewell answer I created a couple of alias functions that will do it for you. Just add these to your shell startup file

    #list available jdks
    alias jdks="/usr/libexec/java_home -V"
    # jdk version switching - e.g. `jdk 6` will switch to version 1.6
    function jdk() { 
      echo "Switching java version"; 
      export JAVA_HOME=`/usr/libexec/java_home -v 1.$1`; 
      java -version; 
    }
    

    https://gist.github.com/Noyabronok/0a90e1f3c52d1aaa941013d3caa8d0e4

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

    macOS El Capitan or newer will choose the higher version of the JDK available in /Library/Java/JavaVirtualMachines, so in order to downgrade you may rename the file Info.plist to something else like Info.plist.disabled so that the OS will choose a previous version.

    0 讨论(0)
  • 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".
    
    0 讨论(0)
提交回复
热议问题