如何更改mac上的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`

其他回答

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)

基于@markhellewell的答案,我创建了几个别名函数,将为您做这件事。只需将这些添加到您的shell启动文件

#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

在不需要安装额外工具的情况下,让/usr/ libeexec /java_home -V将单独安装的JDK识别为一级成员的简单方法是如下所示的符号链接:

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

<path>是一个包含Contents/Home/bin等子目录的目录。


一个常见的用例是注册Android Studio中包含的JDK/JRE:

在macOS上最新版本的Android Studio中,Java工具的默认位置是/Applications/Android\ Studio.app/Contents/jre。我们将使用jdk-android-studio。JDK作为标识符:

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

现在,/usr/libexec/java_home -V将在匹配Java虚拟机下列出它:

$ /usr/libexec/java_home -V
Matching Java Virtual Machines (1):
    11.0.13 (arm64) "JetBrains s.r.o." - "OpenJDK 11.0.13" /Applications/Android Studio.app/Contents/jre/Contents/Home
/Applications/Android Studio.app/Contents/jre/Contents/Home

首先找出存储环境变量的位置

emacs bash_profile zshrc中

设置环境变量步骤:- .单击“确定”

从JAVA下载jdk 双击安装它 现在在文件中设置环境变量 a. emacs。你可以使用这个链接或看到下面的截图

b.对于ZSH配置文件设置-

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)

所有设置现在您可以轻松升级或降级JAVA版本..

太简单的解决方法:真让人头疼——这是一个快速简单的解决方法,对我来说很有效。

Mac OS Sierra版本10.12.13

使用快捷键:CMD+SHIFT+G -输入"/Library/" 找到JAVA文件夹 右键单击Java文件夹=移动到废纸篓(需要密码) 安装: Java SE开发工具包8 jdk-8u131-macosx-x64。下载Javascript SDK 确保新的JAVA文件夹出现在/LIBRARY/中 安装Eclipse |为Java开发人员安装Eclipse IDE 繁荣做