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


当前回答

使用jenv,它就像一个Java环境管理器。它非常容易使用和清洁

对于Mac,请按照以下步骤操作:

brew install jenv

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

安装:如果您正在使用bash,请遵循以下步骤:

$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile

echo 'eval "$(jenv init -)"' >> ~/.bash_profile

$ exec $SHELL -l

向jenv添加所需的JVM版本:

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

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

检查已安装版本:

jenv versions

设置您想要使用的Java版本:

jenv global oracle64-1.6.0

其他回答

如果你用的是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

这有点棘手,但请尝试遵循在OS X 10.9 (Mavericks)上安装Java中描述的步骤。基本上,你得把你的别名更新为java。

循序渐进:

在安装JDK 1.7之后,您将需要执行sudo ln -snf命令来更改到当前java的链接。要做到这一点,打开Terminal并发出命令:

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

注意,目录jdk1.7.0_51. conf。jdk可能会根据您安装的SDK版本而变化。

现在,您需要将JAVA_HOME设置为指向jdk_1.7.0_xx的位置。安装JDK。再次打开终端,输入:

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

您可以在您的.bashrc文件中添加上面的export JAVA_HOME行,以使java永久地存在于您的终端中

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)

来自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`

将此函数添加到bashrc或zshrc, java-change [version]以选择JDK

# set and change java versions
function java-change() {
  echo "----- old java version -----"
  java -version
  if [ $# -ne 0 ]; then
    export JAVA_HOME=`/usr/libexec/java_home -v $@`
    export PATH=$JAVA_HOME/bin:$PATH
  fi
  echo "----- new java version -----"
  java -version
}