如何更改mac上的Java默认版本?


当前回答

首先运行/usr/ libeexec /java_home -V,输出如下:

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

选择你想要的默认版本(1.6.0_65-b14-462),然后:

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

或者你可以只指定主版本,比如:

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

现在当你运行java -version时,你会看到:

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)

在shell的init文件中添加export JAVA_HOME…行。

对于Bash(如antonyh所述):

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

鱼(如ormurin所述)

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

更新.zshrc文件应该工作:

nano ~/.zshrc

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

按“CTRL+X”退出编辑器 按Y保存更改

source ~/.zshrc
echo $JAVA_HOME
java -version

其他回答

如果你用的是fish,而你用的是mac,你希望能够在JDK之间切换,那么下面对我来说适用于mac。

@kenglxn的答案对我不起作用,我发现它没有设置-g,这是全局的!

放在~/.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

要了解您已安装的版本/次要版本,您可以执行以下操作:

/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

JDK Switch脚本

我改编了上面@Alex的答案,并编写了以下代码来修复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;
}

切换到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)

切换到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)

检查Java版本:Java -version

版本切换:https://devqa.io/brew-install-java/

开放的~ / . bash_profile

export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)

export JAVA_11_HOME=$(/usr/libexec/java_home -v11)

别名java8=“出口JAVA_HOME=$ java_8_homs” 别名java11=“出口JAVA_HOME=$ java_11_homs”

源~ / . bash_profile

当我们切换到java11或java8时,java -version命令没有显示正确的版本。

在这种情况下,使用mvn -version查看用于构建解决方案的正确java版本

来自Apple官方java_home(1)手册页:

**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`

我使用我写的fish函数

function javav
  set min_version 8
  set max_version 99
  set java_home_cmd '/usr/libexec/java_home 2>/dev/null --failfast --version'

  if ! test (eval $java_home_cmd $argv)
    echo "Version not found"
    return 1
  end

  for current_version in (seq $min_version $max_version)
    set path_to_remove (eval $java_home_cmd $current_version)
    if ! test -z $path_to_remove
      echo 'Removing' $path_to_remove 'from PATH'
      set PATH (string match --invert $path_to_remove/bin $PATH)
    end
  end

  echo 'Setting up env for Java' $argv
  set -x JAVA_HOME (eval $java_home_cmd $argv)
  set PATH $JAVA_HOME/bin $PATH
end

它基本上自动化了这个答案中的步骤,类似于这个,但也负责设置PATH。

只要把它放在.config/fish/functions/中,然后像这样使用它:

javav 11 # Sets to Java 11
javav 16 # Sets to Java 16