简体   繁体   English

如何在 macOS 上设置或更改默认 Java (JDK) 版本?

[英]How to set or change the default Java (JDK) version on macOS?

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

First run /usr/libexec/java_home -V which will output something like the following:首先运行/usr/libexec/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

Pick the version you want to be the default ( 1.6.0_65-b14-462 for arguments sake) then:选择您想要的默认版本( 1.6.0_65-b14-462用于参数)然后:

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时,您将看到:

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.export JAVA_HOME…行添加到 shell 的 init 文件中。

For Bash (as stated by antonyh ):对于 Bash(如antonyh 所述):

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

For Fish (as stated by ormurin)对于鱼(如 ormurin 所述)

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

Updating the .zshrc file should work:更新 .zshrc 文件应该可以工作:

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按 CTRL+X 退出编辑器 按 Y 保存更改

source ~/.zshrc
echo $JAVA_HOME
java -version

This answer is an attempt to address: how to control java version system-wide (not just in currently running shell) when several versions of JDK are installed for development purposes on macOS El Capitan or newer (Sierra, High Sierra, Mojave) .这个答案试图解决:当在macOS El Capitan 或更新版本(Sierra、High Sierra、Mojave)上安装多个版本的 JDK以用于开发目的时,如何在系统范围内控制 java 版本(不仅仅是在当前运行的 shell 中)。 As far as I can tell, none of the current answers do that (*).据我所知,当前的答案都没有做到这一点(*)。

As a developer, I use several JDKs, and I want to switch from one to the other easily.作为一名开发人员,我使用了几个 JDK,我想轻松地从一个切换到另一个。 Usually I have the latest stable one for general use, and others for tests.通常我有最新的稳定版用于一般用途,其他的用于测试。 But I don't want the system (eg when I start my IDE) to use the latest "early access" version I have for now.但我不希望系统(例如,当我启动我的 IDE 时)使用我目前拥有的最新“抢先体验”版本。 I want to control system's default, and that should be latest stable.我想控制系统的默认值,那应该是最新稳定的。

The following approach works with Java 7 to 12 at least (early access at the time of this writing), with Oracle JDK or OpenJDK (including builds by AdoptOpenJDK produced after mid-October 2018 ).以下方法至少适用于Java 7 到 12 (在撰写本文时早期访问),适用于Oracle JDKOpenJDK (包括AdoptOpenJDK 在 2018 年 10 月中旬之后生成的构建)。

Solution without 3rd party tools:没有第三方工具的解决方案:

  • leave all JDKs at their default location, under /Library/Java/JavaVirtualMachines .将所有 JDK 保留在/Library/Java/JavaVirtualMachines下的默认位置。 The system will pick the highest version by default.系统默认选择最高版本。
  • To exclude a JDK from being picked by default, rename its Contents/Info.plist to Info.plist.disabled .要在默认情况下排除 JDK,请将其Contents/Info.plist重命名为Info.plist.disabled That JDK can still be used when $JAVA_HOME points to it, or explicitly referenced in a script or configuration.$JAVA_HOME指向 JDK 或在脚本或配置中显式引用时,该 JDK 仍然可以使用。 It will simply be ignored by system's java command.它只会被系统的java命令忽略。

System launcher will use the JDK with highest version among those that have an Info.plist file.系统启动器将使用具有Info.plist文件的最高版本的 JDK。

When working in a shell with alternate JDK, pick your method among existing answers ( jenv , or custom aliases/scripts around /usr/libexec/java_home , etc).在使用备用 JDK 的 shell 中工作时,请在现有答案( jenv/usr/libexec/java_home周围的自定义别名/脚本等)中选择您的方法。


Details of investigation in this gist .调查细节在此要点


(*) Current answers are either obsolete (no longer valid for macOS El Capitan or Sierra), or only address a single JDK, or do not address the system-wide aspect. (*) 当前答案要么已过时(不再对 macOS El Capitan 或 Sierra 有效),要么仅针对单个 JDK,或者不针对系统范围的方面。 Many explain how to change $JAVA_HOME , but this only affects the current shell and what is launched from there.许多人解释了如何更改$JAVA_HOME ,但这只会影响当前的 shell 以及从那里启动的内容。 It won't affect an application started from OS launcher (unless you change the right file and logout/login, which is tedious).它不会影响从操作系统启动器启动的应用程序(除非您更改正确的文件并注销/登录,这很乏味)。 Same for jenv, it's cool and all, but as far as I can tell it merely changes environment variables, so it has the same limitation. jenv 也一样,它很酷,但据我所知,它只是改变了环境变量,所以它有同样的限制。

Adding to the above answers, I put the following lines in my .bash_profile (or .zshrc for MacOS 10.15+) which makes it really convenient to switch (including @elektromin's comment for java 9):除了上述答案之外,我还在我的.bash_profile (或 MacOS .zshrc的 .zshrc )中添加了以下几行,这使得切换非常方便(包括@elektromin 对 java 9 的评论):

alias j12="export JAVA_HOME=`/usr/libexec/java_home -v 12`; java -version"
alias j11="export JAVA_HOME=`/usr/libexec/java_home -v 11`; java -version"
alias j10="export JAVA_HOME=`/usr/libexec/java_home -v 10`; java -version"
alias j9="export JAVA_HOME=`/usr/libexec/java_home -v 9`; java -version"
alias j8="export JAVA_HOME=`/usr/libexec/java_home -v 1.8`; java -version"
alias j7="export JAVA_HOME=`/usr/libexec/java_home -v 1.7`; java -version"

After inserting, execute $ source .bash_profile插入后,执行$ source .bash_profile

I can switch to Java 8 by typing the following:我可以通过键入以下内容切换到 Java 8:

$ j8
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)

A small fish function based on /usr/libexec/java_home一个基于/usr/libexec/java_home的小鱼函数

function jhome
    set JAVA_HOME (/usr/libexec/java_home $argv)
    echo "JAVA_HOME:" $JAVA_HOME
    echo "java -version:"
    java -version
end

If you don't use fish, you can do something similar in bash:如果你不使用fish,你可以在bash中做类似的事情:

#!/bin/bash

jhome () {
  export JAVA_HOME=`/usr/libexec/java_home $@`
  echo "JAVA_HOME:" $JAVA_HOME
  echo "java -version:"
  java -version
}

Then to switch between javas do:然后在javas之间切换:

$> jhome           #switches to latest java
$> jhome -v 1.7    #switches to java 1.7
$> jhome -v 1.6    #switches to java 1.6

ref: https://gist.github.com/kenglxn/1843d552dff4d4233271参考: https ://gist.github.com/kenglxn/1843d552dff4d4233271

Use jenv is an easy way.使用jenv是一种简单的方法。 (Update 2022) (2022 年更新)

  1. Install jenv: see Getting started安装 jenv:请参阅入门

  2. Install java with brew用brew安装java

     brew install openjdk@11 ln -s /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk #other java brew install openjdk@8 brew install openjdk@17
  3. Add java to jenv将 java 添加到 jenv

     jenv add /Library/Java/JavaVirtualMachines/openjdk-11.jdk/Contents/Home
  4. Use: refer to jenv使用:参考jenv

It is a little bit tricky, but try to follow the steps described in Installing Java on OS X 10.9 (Mavericks) .这有点棘手,但请尝试按照在 OS X 10.9 (Mavericks) 上安装 Java 中描述的步骤进行操作。 Basically, you gonna have to update your alias to java .基本上,您必须将别名更新为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 .安装 JDK 1.7 后,您需要执行sudo ln -snf以将链接更改为当前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.请注意,目录jdk1.7.0_51.jdk可能会根据您安装的 SDK 版本而改变。

Now, you need to set JAVA_HOME to point to where jdk_1.7.0_xx.jdk was installed.现在,您需要将JAVA_HOME为指向jdk_1.7.0_xx.jdk的安装位置。 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您可以在.bashrc文件中添加上面的export JAVA_HOME行,以便在终端中永久保存 java

install JDK, not just JRE安装 JDK,而不仅仅是 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. TextEdit 将显示一个空白页面,您可以填写该页面。

add to doc: export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home添加到文档: 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

Update...更新...

With macOS Monterey, v12.0.1 Cask is no longer a Brew command.在 macOS Monterey 中,v12.0.1 Cask 不再是 Brew 命令。 Just follow the steps but in step 4 just do $ brew install ... or $ brew install --cask ... instead of $ brew cask install ... Thanks to @Shivam Sahil comment.只需按照步骤操作,但在步骤 4 中只需执行$ brew install ...$ brew install --cask ...而不是$ brew cask install ...感谢@Shivam Sahil评论。

I will share my experiences with macOS Big Sur v11.4, the best way to deal with these problems is by installing java using Homebrew:我将分享我使用 macOS Big Sur v11.4 的经验,处理这些问题的最佳方法是使用 Homebrew 安装 java:

1 - Install Homebrew. 1 - 安装自制软件。

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2 - Install Homebrew Cask. 2 - 安装 Homebrew Cask。

$ brew tap homebrew/cask-versions 
$ brew update 
$ brew tap homebrew/cask

3 - Install the latest version of Java 3 - 安装最新版本的 Java

$ brew cask install java

4 - Install the other needed versions of Java (Java8, Java11, Java13). 4 - 安装其他需要的 Java 版本(Java8、Java11、Java13)。

$ brew tap adoptopenjdk/openjdk

$ brew cask install adoptopenjdk8
$ brew cask install adoptopenjdk11
$ brew cask install adoptopenjdk13
$ brew cask install adoptopenjdk14

5 - Switch between different versions of Java 5 - 在不同版本的 Java 之间切换

Switching between different versions of Java, you only need to add the following to your .bash_profile or .zshrc .在不同版本的 Java 之间切换,您只需将以下内容添加到您的.bash_profile.zshrc中。

In this case, we want to be able to switch between Java8, Java11, Java13 and Java14:在这种情况下,我们希望能够在 Java8、Java11、Java13 和 Java14 之间切换:

export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
export JAVA_13_HOME=$(/usr/libexec/java_home -v13)
export JAVA_14_HOME=$(/usr/libexec/java_home -v14)

alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME'
alias java13='export JAVA_HOME=$JAVA_13_HOME'
alias java14='export JAVA_HOME=$JAVA_14_HOME'

# default to Java 14
java14

6 - Reload .bash_profile or .zshrc for the aliases to take effect: 6 - 重新加载.bash_profile.zshrc以使别名生效:

$ source ~/.bash_profile

or或者

$ source ~/.zshrc

7 - Finally you can use the aliases to switch between different Java versions. 7 - 最后,您可以使用别名在不同的 Java 版本之间切换。

$ java8
$ java -version
java version "1.8.0_261"

For more info you can see this post: How to Use Brew to Install Java on Mac有关更多信息,您可以查看这篇文章:如何使用 Brew 在 Mac 上安装 Java

tl;dr tl;博士

Add the line:添加行:

export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home'

to the file到文件

~/.bash_profile

(replace jdk1.8.0_144.jdk with your downloaded version) (将 jdk1.8.0_144.jdk 替换为您下载的版本)

then source ~/.bash_profile然后source ~/.bash_profile

Use jenv, it is like a Java environment manager.使用 jenv,它就像一个 Java 环境管理器。 It is super easy to use and clean它非常易于使用和清洁

For Mac, follow the steps:对于 Mac,请执行以下步骤:

brew install jenv

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

Installation: If you are using bash follow these steps:安装:如果您使用的是 bash,请按照以下步骤操作:

$ 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:将所需的 JVM 版本添加到 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:通过以下方式设置您要使用的 Java 版本:

jenv global oracle64-1.6.0

Four easy steps using terminal for people who uses the default process.. :)对于使用默认进程的人来说,使用终端的四个简单步骤.. :)

  1. echo $JAVA_HOME gives you current java home. echo $JAVA_HOME为您提供当前的 java home。 For eg: /Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/例如: /Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/

  2. cd /Library/Java/JavaVirtualMachines/ will take you to the folder where u normally install jdks (It might be different for your machines) cd /Library/Java/JavaVirtualMachines/将带您到您通常安装 jdks 的文件夹(您的机器可能会有所不同)

  3. ls shows you available folders (normally it will have the version numbers, for eg: jdk1.8.0_191.jdk openjdk-11.0.2.jdk ) ls显示可用的文件夹(通常会有版本号,例如: jdk1.8.0_191.jdk openjdk-11.0.2.jdk
  4. export JAVA_HOME='/Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home' will change the java home.. export JAVA_HOME='/Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home'将改变java home..

macOS El Capitan或更高版本将选择/Library/Java/JavaVirtualMachines中可用的更高版本的 JDK,因此为了降级,您可以将文件Info.plist重命名为Info.plist.disabled之类的其他名称,以便操作系统选择以前的版本。

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...把它放在你的 ~/.profile 中,并在你的终端中使用它,比如setjdk 1.8setjdk 1.7setjdk 9等等......

If you don't have removeFromPath then it is:如果您没有removeFromPath那么它是:

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

Consider the following approach only to change the JDK for each and specific tab of your terminal (ie: iTerm ).考虑以下方法来更改终端的每个特定选项卡的 JDK(即: iTerm )。

Having in the /Library/Java/JavaVirtualMachines path the two following jdks/Library/Java/JavaVirtualMachines路径中有以下两个 jdks

  • openjdk8u275-b01
  • openjdk-11.0.9.1+1

And in the .bash_profile file the following:.bash_profile文件中如下:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/openjdk-11.0.9.1+1/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH

If you open Iterm - with the first Tab A - and the following commands are executed:如果您打开Iterm - 使用第一个选项卡 A - 并执行以下命令:

javac -version
javac 11.0.9.1

java -version
openjdk version "11.0.9.1" 2020-11-04
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.9.1+1)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.9.1+1, mixed mode)

The output is correct and expected输出正确且符合预期

But if you open a second Tab B and you need override the default JDK then write in the terminal the following:但是,如果您打开第二个选项卡 B并且需要覆盖默认JDK,则在终端中写入以下内容:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/openjdk8u275-b01/Contents/Home/
export PATH=$JAVA_HOME/bin:$PATH

Then然后

javac -version
javac 1.8.0_275

java -version
openjdk version "1.8.0_275"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_275-b01)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.275-b01, mixed mode)

It works fine.它工作正常。 Of course if the Tab B is closed or you open a new Tab C all work according the .bash_profile settings (therefore the default settings)当然,如果选项卡 B已关闭或您打开一个新选项卡 C ,所有工作都根据.bash_profile设置(因此默认设置)

You can add it to your .bash_profile to have the version set by default.您可以将其添加到您的 .bash_profile 以默认设置版本。

//Open bash profile
open ~/.bash_profile

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

// run bash profile
source ~/.bash_profile

将以下命令添加到~/.zshenv文件

export JAVA_HOME=`/usr/libexec/java_home -v 1.8` 
  1. Add the following line of code to your .zshrc (or bash_profile ):将以下代码行添加到您的.zshrc (或bash_profile ):

alias j='f(){ export JAVA_HOME= /usr/libexec/java_home -v $1 };f'别名 j='f(){ 导出 JAVA_HOME= /usr/libexec/java_home -v $1 };f'

  1. Save to session:保存到会话:

$ source .zshrc $源.zshrc

  1. Run command (eg j 13, j14, j1.8...)运行命令(例如 j 13、j14、j1.8...)

$ j 1.8 $ j 1.8

Explanation This is parameterised so you do not need to update the script like other solutions posted.说明这是参数化的,因此您不需要像发布的其他解决方案那样更新脚本。 If you do not have the JVM installed you are told.如果你没有安装 JVM,你会被告知。 Sample cases below:以下示例案例:

/Users/user/IDE/project $ j 1.8
/Users/user/IDE/project $ java -version
openjdk version "1.8.0_265"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_265-b01)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.265-b01, mixed mode)
/Users/user/IDE/project $ j 13
/Users/user/IDE/project $ java -version
openjdk version "13.0.2" 2020-01-14
OpenJDK Runtime Environment (build 13.0.2+8)
OpenJDK 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)
/Users/user/IDE/project $ j 1.7
Unable to find any JVMs matching version "1.7".

From the Apple's official java_home(1) man page :来自 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 Script JDK 切换脚本

I have adapted the answer from @Alex above and wrote the following to fix the code for Java 9.我已经修改了上面@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;
}

Switch to Java 8切换到 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)

Switch to Java 9切换到 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)

Add following in your ~/.bash_profile and set the default java version accordingly.在您的~/.bash_profile中添加以下内容并相应地设置默认 java 版本。

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

alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME'

# default to Java 8
java8

I am using macOS and have installed java using brew .我正在使用 macOS 并使用brew安装了 java。

Edit: If you are not using bash please update the correct shell file, example, if you are using zsh then it will be ~/.zshrc编辑:如果您不使用bash ,请更新正确的 shell 文件,例如,如果您使用的是zsh ,那么它将是~/.zshrc

If still u are not able to set it.如果仍然无法设置它。 using this command.使用这个命令。

export JAVA_HOME= /usr/libexec/java_home -v 1.8导出 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)导出 JAVA_HOME= $(/usr/libexec/java_home -v 1.8)

it will surely work.它肯定会奏效。

First find out where do you store the environment variables-首先找出你在哪里存储环境变量 -

  1. emacs emacs
  2. bash_profile bash_profile
  3. zshrc file .zshrc 文件

Steps to Set up the environment variable :-设置环境变量的步骤:-

  1. Download the jdk from JAVA从 JAVA 下载 jdk

  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对于emacs.profile ,您可以使用此链接或查看下面的屏幕截图

在此处输入图像描述

b.湾。 For ZSH profile setup -对于 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)

All set Now you can easily upgrade or degrade the JAVA version..全部设置现在您可以轻松升级或降级 JAVA 版本..

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.如果您使用的是fish并且您使用的是mac并且您希望能够在JDK之间切换,那么下面的内容适用于我在mac上。

@kenglxn's answer didn't work for me and I figured out it bcos didn't set -g which is global ! @kenglxn 的回答对我不起作用,我发现它 bcos 没有设置 -g 这是全球性的!

Put below under ~/.config/fish/config.fish放在下面 ~/.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

如果您有多个版本并且想要使用特定版本来运行某些东西,请使用以下示例:

/usr/libexec/java_home -v 1.7.0_75 --exec java -jar you-file.jar

Previously I used alias'es in .zshrc for easy switching between versions but today I use SDKMAN.以前我在 .zshrc 中使用别名来轻松切换版本,但今天我使用 SDKMAN。 SDKMAN can also handle setting default java for the system, and downloading and installing new java versions. SDKMAN 还可以处理为系统设置默认java,以及下载和安装新的java 版本。

Once sdkman is installed you can then do commands similar to what is possible with the nvm tool for handling node versions.安装 sdkman 后,您可以执行类似于使用 nvm 工具处理节点版本的命令。

sdk list java will list the java versions available on your system as well as available online for installation including their identifier that you can use in the sdk use , sdk default and sdk install commands. sdk list java将列出您系统上可用的 java 版本以及可在线安装的 java 版本,包括您可以在sdk usesdk defaultsdk install命令中使用的标识符。

eg to install Amazon Corretto 11.0.8 and ask if it should be the new default do this: sdk install java 11.0.8-amzn例如安装 Amazon Corretto 11.0.8 并询问它是否应该是新的默认设置: sdk install java 11.0.8-amzn

A feature I also use regularly is the .sdkmanrc file.我也经常使用的一个特性是.sdkmanrc文件。 If you place that in a directory on your machine and run the sdk env command in the directory then you can configure tool versions used only in that directory.如果您将它放在您机器上的一个目录中并在该目录中运行sdk env命令,那么您可以配置仅在该目录中使用的工具版本。 It is also possible to make sdkman switch tool versions automatically using the sdkman_auto_env=true configuration.也可以使用sdkman_auto_env=true配置自动使 sdkman 切换工具版本。

sdkman also supports handling other tools for the JVM such as gradle, kotlin, maven and more. sdkman 还支持处理 JVM 的其他工具,例如 gradle、kotlin、maven 等。

For more information check out https://sdkman.io/usage#env有关更多信息,请查看https://sdkman.io/usage#env

Based on @markhellewell answer I created a couple of alias functions that will do it for you.基于@markhellewell 的回答,我创建了几个别名函数来为你做这件事。 Just add these to your shell startup file只需将这些添加到您的 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 https://gist.github.com/Noyabronok/0a90e1f3c52d1aaa941013d3caa8d0e4

Here is how I do it on my Linux (Ubuntu / Mint mate) , I guess Mac can do it similarly.这是我在我的Linux (Ubuntu / Mint mate)上的做法,我猜 Mac 也可以这样做。


Install & config安装和配置

Steps:脚步:

  • [Basic - part] [基础-部分]
  • Download jdk (the .tgz file) by hand.手动下载 jdk .tgz文件)
  • Uncompress & rename properly, at a proper location.在适当的位置正确解压缩和重命名。
    eg /mnt/star/program/java/jdk-1.8例如/mnt/star/program/java/jdk-1.8
  • Make a soft link, which will be changed to switch java version later.做一个软链接,后面改成切换java版本。
    eg ln -s /mnt/star/program/java/jdk-1.8 /mnt/star/program/java/java例如ln -s /mnt/star/program/java/jdk-1.8 /mnt/star/program/java/java
    Thus /mnt/star/program/java/java is the soft link.因此/mnt/star/program/java/java是软链接。
  • Set JAVA_HOME in a start script.在启动脚本中设置JAVA_HOME
    Could use file like /etc/profile.d/eric.sh , or just use ~/.bashrc .可以使用/etc/profile.d/eric.sh类的文件,或者只使用~/.bashrc
    eg JAVA_HOME=/mnt/star/program/java/java例如JAVA_HOME=/mnt/star/program/java/java
  • Then open a new bash shell.然后打开一个新的 bash shell。 java -version should print the java version. java -version应该打印 java 版本。
  • [More version - part] [更多版本-部分]
  • Download & install more Java version, as need, similar as above steps.根据需要下载并安装更多 Java 版本,与上述步骤类似。
    eg例如
    /mnt/star/program/java/jdk-11
  • [Switch - part] [开关-部分]
  • In ~/.bashrc , define variable for various Java version.~/.bashrc中,为各种 Java 版本定义变量。
    eg例如
    _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.~/.bashrc中,定义切换 Java 版本的命令。
    eg例如
    ## 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.在终端中, source ~/.bashrc使更改生效。
  • Then could switch using the defined commands.然后可以使用定义的命令进行切换。

Commands - from above config命令 - 从上面的配置

Commands:命令:

  • jv11
    Switch to Java 11切换到 Java 11
  • jv8
    Switch to Java 8切换到 Java 8
  • jvd
    Switch to default Java version, which is denoted by _E_JAVA_HOME_D defined above.切换到默认的 Java 版本,由上面定义的_E_JAVA_HOME_D表示。
  • jv
    Show java version.显示 java 版本。

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 .它通过更改用作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.在我的机器上手动安装jdk时,我保留了次要版本,然后与主要版本建立了软链接,但没有次要版本。
    eg例如
    // this is the actual dir, // 这是实际的目录,
    jdk1.8.0_191

    // this is a soft link to jdk1.8.0_191 // 这是一个指向jdk1.8.0_191的软链接
    jdk-8

    // this is a soft link to jdk-8 or jdk-11 // 这是一个指向jdk-8jdk-11的软链接
    java

  • I define command alias in ~/.bashrc , but define variable in a separate file.我在~/.bashrc中定义命令别名,但在单独的文件中定义变量。
    I am using ~/.eric_var to define the variables, and ~/.bashrc will source it (eg source $HOME/.eric_var ).我正在使用~/.eric_var来定义变量,并且~/.bashrc将获取它(例如source $HOME/.eric_var )。

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:/usr/libexec/java_home -V给出的列表中包含单独安装的 JDK 的一种简单方法是对目录进行符号链接,如下所示:

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:例如,要注册 Android Studio 随附的 JDK(撰写本文时为 1.8.0_152),请使用:

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.现在java_home将在Matching Java Virtual Machines下列出 JDK,您可以如上所述选择它。

add this function to bashrc or zshrc, java-change [version] to choose the JDK在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
}

Check Java version: java -version检查Java版本:java -version

Switch between versions: https://devqa.io/brew-install-java/在版本之间切换: https ://devqa.io/brew-install-java/

open ~/.bash_profile打开 ~/.bash_profile

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

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

alias java8='export JAVA_HOME=$JAVA_8_HOME' alias java11='export JAVA_HOME=$JAVA_11_HOME'别名 java8='export JAVA_HOME=$JAVA_8_HOME' 别名 java11='export JAVA_HOME=$JAVA_11_HOME'

source ~/.bash_profile源〜/ .bash_profile

When we are switching to java11 or java8, java -version command is not showing the correct version.当我们切换到 java11 或 java8 时,java -version 命令没有显示正确的版本。

In that case use mvn -version to see the correct java version is used for building the solution在这种情况下,使用 mvn -version 查看用于构建解决方案的正确 java 版本

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

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

Very simple answer:非常简单的答案:

  1. Run and get all the installed JDK path运行并获取所有已安装的JDK路径
/usr/libexec/java_home -V
  1. Run direct commands from jenv.bejenv.be运行直接命令
git clone https://github.com/jenv/jenv.git ~/.jenv #Linux/macOS
OR
brew install jenv #macOS

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

jenv add PATH_FROM_STEP_1

I'm using this fish function I wrote :我正在使用我写的这个功能:

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

It basically automates the step in this answer , similarly to this one , but also taking care of setting the PATH.它基本上自动化了这个答案中的步骤,类似于这个,但也负责设置 PATH。

Just put it in .config/fish/functions/ and then use it like this:只需将它放在.config/fish/functions/中,然后像这样使用它:

javav 11 # Sets to Java 11
javav 16 # Sets to Java 16
  1. Open terminal打开终端
  2. Paste: nano ~/.zshrc粘贴: nano ~/.zshrc
  3. Set your installed Java version (for example used 11.0.11 ): export JAVA_HOME=$(/usr/libexec/java_home -v 11.0.11)设置您安装的 Java 版本(例如使用11.0.11 ): export JAVA_HOME=$(/usr/libexec/java_home -v 11.0.11)
  4. Save file with control+X .使用control+X保存文件。 When it prompts just type "Y"当它提示时只需输入"Y"
  5. After that hit on Return and you are ready to go.在点击Return之后,您就可以开始了。
  6. Don't forget to check result: java -version不要忘记检查结果: java -version

TOO EASY SOLUTION: What a headache - this was a quick easy solution that worked for me.太简单的解决方案:多么令人头疼 - 这是一个对我有用的快速简单的解决方案。

Mac OS Sierra Version 10.12.13 Mac OS Sierra 版本 10.12.13

  1. Use the shortcut keys: CMD+SHIFT+G - type in "/Library/"使用快捷键:CMD+SHIFT+G - 输入“/Library/”

  2. Find the JAVA folder找到 JAVA 文件夹

  3. Right Click Java Folder = Move to Trash (Password Required)右键单击 Java 文件夹 = 移至垃圾箱(需要密码)

  4. Install: Java SE Development Kit 8 jdk-8u131-macosx-x64.dmg |安装:Java SE 开发工具包 8 jdk-8u131-macosx-x64.dmg | Download Javascript SDK 下载 JavaScript SDK

  5. Make sure the new JAVA folder appears in /LIBRARY/确保新的 JAVA 文件夹出现在 /LIBRARY/
  6. Install Eclipse |安装 Eclipse | Install Eclipse IDE for Java Developers 为 Java 开发人员安装 Eclipse IDE
  7. Boom Done繁荣完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM