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

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

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

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

    I have done it for the local terminal with

    export JAVA_8_HOME=(/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/)
    export JAVA_13_HOME=(/Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home/)
    
    alias java8='export JAVA_HOME=$JAVA_8_HOME; alias java="$JAVA_8_HOME/bin/java"; $JAVA_8_HOME/bin/java -version'
    alias java13='export JAVA_HOME=$JAVA_13_HOME; alias java="$JAVA_13_HOME/bin/java"; $JAVA_13_HOME/bin/java -version'
    
    export JAVA_HOME=$JAVA_13_HOME
    
    0 讨论(0)
  • 2020-11-22 16:07

    You can add it to your .bash_profile to have the version set by default.

    //Open bash profile
    open ~/.bash_profile
    
    export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
    
    // run bash profile
    source ~/.bash_profile
    
    0 讨论(0)
  • 2020-11-22 16:07

    First find out where do you store the environment variables-

    1. emacs
    2. bash_profile
    3. zshrc file

    Steps to Set up the environment variable :-

    1. Download the jdk from JAVA

    2. install it by double click

    3. Now set-up environment variables in your file

      a. For emacs.profile you can use this link OR see the screenshot below

    b. For ZSH profile setup -

    1. export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home
    
    2. source ~/.zshrc - Restart zshrc to reflect the changes.
    
    3. echo $JAVA_HOME - make sure path is set up properly 
       ----> /Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home
    
    4. java -version 
    
       -->  java version "1.8.0_112"  Java(TM) SE Runtime Environment (build 1.8.0_112-b16)Java HotSpot(TM) 64-Bit Server VM (build 25.112-b16, mixed mode)
    

    All set Now you can easily upgrade or degrade the JAVA version..

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

    If still u are not able to set it. using this command.

    export JAVA_HOME=/usr/libexec/java_home -v 1.8

    then you have to use this one.

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

    it will surely work.

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

    First run /usr/libexec/java_home -V which will output something like the following:

    Matching Java Virtual Machines (3):
    1.8.0_05, x86_64:   "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
    1.6.0_65-b14-462, x86_64:   "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
    1.6.0_65-b14-462, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
    
    /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
    

    Pick the version you want to be the default (1.6.0_65-b14-462 for arguments sake) then:

    export JAVA_HOME=`/usr/libexec/java_home -v 1.6.0_65-b14-462`
    

    or you can specify just the major version, like:

    export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
    

    Now when you run java -version you will see:

    java version "1.6.0_65"
    Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
    Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)
    

    Add the export JAVA_HOME… line to your shell’s init file.

    For Bash (as stated by antonyh):

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

    For Fish (as stated by ormurin)

    set -x JAVA_HOME (/usr/libexec/java_home -d64 -v1.8)
    

    Updating the .zshrc file should work:

    nano ~/.zshrc
    
    export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0)
    

    Press CTRL+X to exit the editor Press Y to save your changes

    source ~/.zshrc
    echo $JAVA_HOME
    java -version
    
    0 讨论(0)
  • 2020-11-22 16:12
    function setjdk() {
      if [ $# -ne 0 ]; then
        removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
        if [ -n "${JAVA_HOME+x}" ]; then
          removeFromPath $JAVA_HOME
        fi
        export JAVA_HOME=`/usr/libexec/java_home -v $@`
        export PATH=$JAVA_HOME/bin:$PATH
      fi
    }
    

    put this in your ~/.profile and use it in your terminal like so setjdk 1.8, setjdk 1.7, setjdk 9 etc etc...

    If you don't have removeFromPath then it is:

    function removeFromPath() { export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;") }

    0 讨论(0)
提交回复
热议问题