29

Mac上的JDK多版本管理

 4 years ago
source link: https://www.tuicool.com/articles/QrYNBje
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

yQBbE3i.png!web 我的Mac上已经有一个JDK8的版本了,这不 JDK13 刚发布(2019-09-17),想快速的尝一尝鲜,就得安装多个版本的JDK了。这个对Node、Ruby、Python的使用者来说,已经不是个什么新鲜话题了,但是对于Java的使用者来说,似乎没有那么多的人受到过多版本的折磨(我是通过GitHub上 nvm rbenv pyenv jenv 的Star数量臆测出这个结论的 :P)。

安装JDK 13

通过Homebrew 安装JDK 13,可以先通过 brew cask info java 查看目前Java的版本:

java: 13,33:5b8a42f3905b406298b72d750b6919f6
https://openjdk.java.net/
Not installed
From: https://github.com/Homebrew/homebrew-cask/blob/master/Casks/java.rb
==> Name
OpenJDK Java Development Kit
==> Artifacts
jdk-13.jdk -> /Library/Java/JavaVirtualMachines/openjdk-13.jdk (Generic Artifact)

这里显示的是JDK13,正好是我想要安装的JDK版本,如果不是你想要的版本可以自己搜索相应的 Homebrew Tap。接下来直接安装:

$ brew cask install java
$ java -version

openjdk version "13" 2019-09-17
OpenJDK Runtime Environment (build 13+33)
OpenJDK 64-Bit Server VM (build 13+33, mixed mode, sharing)

这就说明JDK13已经安装好了。

但是另一个问题来了,我电脑上原来安装的JDK8去哪呢?我如何在不同的版本中随意切换呢?比如像Node的 nvm ,Ruby的 rvm ,Python的 pyenv 等。答案是我们可以通过 jenv 来实现相同的效果。

安装 jEnv

  1. 安装 jEnv
    $ brew install jenv
    $ exec $SHELL -l

安装完成之后,然后检查是否安装成功。

$ jenv doctor
[OK] No JAVA_HOME set
[ERROR] Java binary in path is not in the jenv shims.
[ERROR] Please check your path, or try using /path/to/java/home is not a valid path to java installation.
PATH : /usr/local/Cellar/jenv/0.5.2/libexec/libexec:/Users/xxx/.cargo/bin:/Users/xxx/.pyenv/shims:/Users/username/.pyenv:/Users/xxx/.nvm/versions/node/v8.11.4/bin:/Users/xxx/bin:/usr/local/bin:/Users/xxx/.cargo/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Users/xxx/Documents/Projects/golang/bin
[ERROR] Jenv is not loaded in your zsh
[ERROR] To fix : cat eval "$(jenv init -)" >> /Users/xxx/.zshrc

在这里如果按照提示执行 cat eval "$(jenv init -)" >> /Users/xxx/.zshrc :可能会得到如下错误:

cat eval "$(jenv init -)" >> /Users/xxx/.zshrc
cat: eval: No such file or directory
cat: export PATH="/Users/xxx/.jenv/shims:${PATH}"
export JENV_SHELL=zsh
export JENV_LOADED=1
unset JAVA_HOME
source '/usr/local/Cellar/jenv/0.5.2/libexec/libexec/../completions/jenv.zsh'
jenv rehash 2>/dev/null
jenv() {
typeset command
command="$1"
if [ "$#" -gt 0 ]; then
shift
fi

case "$command" in
enable-plugin|rehash|shell|shell-options)
eval `jenv "sh-$command" "$@"`;;
*)
command jenv "$command" "$@";;
esac
}: No such file or directory

经过一番搜索,得到如下的解决办法,主要就是将 cat 替换为 echo ,这里我已经给jEnv提了个 PR ,以消除这个干扰。

  • Bash用户

    $ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
    $ echo 'eval "$(jenv init -)"' >> ~/.bash_profile
    $ exec $SHELL -l
  • Zsh用户

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

然后再次执行 jenv doctor ,得到如下信息:

[OK]	No JAVA_HOME set
[ERROR] Java binary in path is not in the jenv shims.
[ERROR] Please check your path, or try using /path/to/java/home is not a valid path to java installation.
PATH : /usr/local/Cellar/jenv/0.5.2/libexec/libexec:/Users/xxx/.jenv/shims:/Users/xxx/.cargo/bin:/Users/xxx/.pyenv/shims:/Users/username/.pyenv:/Users/xxx/.cargo/bin:/Users/xxx/.pyenv/shims:/Users/username/.pyenv:/Users/xxx/.nvm/versions/node/v8.11.4/bin:/Users/xxx/bin:/usr/local/bin:/Users/xxx/.cargo/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Users/xxx/Documents/Projects/golang/bin:/Users/xxx/Documents/Projects/golang/bin
[OK] Jenv is correctly loaded

为了能够正确的设置 JAVA_HOME ,最好开启 export 插件:

$ jenv enable-plugin export
$ exec $SHELL -l

管理不同版本的JDK

添加JDK

添加最新安装的JDK:

$ jenv add $(/usr/libexec/java_home)

如果 /usr/libexec/java_home 所指的位置不是你想要的,也可以手动指定目录:

$ jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/

查看JDK版本

执行 jenv versions

  system
* 1.8 (set by JENV_VERSION environment variable)
1.8.0.191
13
openjdk64-13
oracle64-1.8.0.191

默认情况下,system指的是系统中安装的最新版本的JDK。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK