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

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

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

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

    If you are using fish and you are using mac and you want to be able to switch between JDK's, then below works for me on mac.

    @kenglxn's answer didn't work for me and I figured out it bcos didn't set -g which is global !

    Put below under ~/.config/fish/config.fish

    alias j8="jhome  -v 1.8.0_162"
    alias j9="jhome  -v 9.0.1"
    
    function jhome
        set -g -x JAVA_HOME (/usr/libexec/java_home $argv)
        echo "JAVA_HOME:" $JAVA_HOME
        echo "java -version:"
        java -version
    end
    
    funcsave jhome
    

    To know which version /minor version you have installed, you can do :

    /usr/libexec/java_home -V                                                                              579ms  Wed 14 Feb 11:44:01 2018
    Matching Java Virtual Machines (3):
        9.0.1, x86_64:  "Java SE 9.0.1" /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home
        1.8.0_162, x86_64:  "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home
        1.8.0_121, x86_64:  "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home
    
    0 讨论(0)
  • 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).

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

    It is a little bit tricky, but try to follow the steps described in Installing Java on OS X 10.9 (Mavericks). Basically, you gonna have to update your alias to java.

    Step by step:

    After installing JDK 1.7, you will need to do the sudo ln -snf in order to change the link to current java. To do so, open Terminal and issue the command:

    sudo ln -nsf /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents \
    /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
    

    Note that the directory jdk1.7.0_51.jdk may change depending on the SDK version you have installed.

    Now, you need to set JAVA_HOME to point to where jdk_1.7.0_xx.jdk was installed. Open again the Terminal and type:

    export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home"
    

    You can add the export JAVA_HOME line above in your .bashrc file to have java permanently in your Terminal

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

    An easy way to include a separately installed JDK in the list given by /usr/libexec/java_home -V is to symlink the directory as follows:

    sudo ln -s path/to/jdk /Library/Java/JavaVirtualMachines/jdk-[some-identifier].jdk
    

    For example, to register the JDK included with Android Studio (1.8.0_152 at the time of writing), use:

    sudo ln -s /Applications/Android\ Studio.app/Contents/jre/jdk /Library/Java/JavaVirtualMachines/jdk-android-studio.jdk
    

    Now java_home will list the JDK under Matching Java Virtual Machines and you can select it as described above.

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

    install JDK, not just JRE

    /usr/libexec/java_home -v 1.8
    

    gives

    /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
    

    next

    touch .bash_profile
    
    open -a TextEdit.app .bash_profile
    

    TextEdit will show you a blank page which you can fill in.

    add to doc: export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home

    in terminal:

    export JAVA_HOME="$(/usr/libexec/java_home -v 1.8)"
    

    try the command:

    javac -version

    should output:

    javac 1.8.0_111
    
    0 讨论(0)
  • 2020-11-22 16:05

    Use jenv, it is like a Java environment manager. It is super easy to use and clean

    For Mac, follow the steps:

    brew install jenv
    
    git clone https://github.com/gcuisinier/jenv.git ~/.jenv
    

    Installation: If you are using bash follow these steps:

    $ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
    
    echo 'eval "$(jenv init -)"' >> ~/.bash_profile
    
    $ exec $SHELL -l
    

    Add desired versions of JVM to jenv:

    jenv add /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
    
    jenv add /System/Library/Java/JavaVirtualMachines/1.8.0.jdk/Contents/Home
    

    Check the installed versions:

    jenv versions
    

    Set the Java version you want to use by:

    jenv global oracle64-1.6.0
    
    0 讨论(0)
提交回复
热议问题