使用Jenv管理Mac上的多个JDK版本

安装jenv

brew install jenv

jenv的全部命令为:

~ » jenv help
Usage: jenv <command> [<args>]

Some useful jenv commands are:
   commands    List all available jenv commands
   local       Set or show the local application-specific Java version
   global      Set or show the global Java version
   shell       Set or show the shell-specific Java version
   rehash      Rehash jenv shims (run this after installing executables)
   version     Show the current Java version and its origin
   versions    List all Java versions available to jenv
   which       Display the full path to an executable
   whence      List all Java versions that contain the given executable

See `jenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/hikage/jenv#readme

配置jenv

因为我使用的是iterm2+zsh,所以我的配置方式是:

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

jenv 基本使用

如果这个时候使用jenv versions查看所有的jdk版本的话,只会看到系统的:

~/.jenv/versions » jenv versions
* system (set by /Users/rollenholt/.jenv/version)

所以需要我们手动将系统上的自己安装的JDK交给jenv来管理。我的电脑上安装了3个JDK, 所以使用jenv add命令来添加他们到jenv中。添加完成以后就
可以使用jenv versions来查看了。可以看到默认情况下每个jdk的版本会有3个label,我删除了其中2个,只保留了一个。

同时在下面的示例中,我本地默认的jdk是9,然后使用jenv local切换为jdk8, 然后使用java -version命令验证生效了。

~/.jenv/versions » ls /Library/Java/JavaVirtualMachines/
jdk-9.0.4.jdk    jdk1.7.0_71.jdk  jdk1.8.0_161.jdk

~/.jenv/versions » jenv add /Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/
oracle64-1.7.0.71 added
1.7.0.71 added
1.7 added
------------------------------------------------------------
~/.jenv/versions » jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/
oracle64-1.8.0.161 added
1.8.0.161 added
1.8 added
------------------------------------------------------------
~/.jenv/versions » jenv add /Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home/
oracle64-9.0.4 added
9.0.4 added
9.0 added
------------------------------------------------------------
~/.jenv/versions » jenv versions
* system (set by /Users/rollenholt/.jenv/version)
  1.7
  1.7.0.71
  1.8
  1.8.0.161
  9.0
  9.0.4
  oracle64-1.7.0.71
  oracle64-1.8.0.161
  oracle64-9.0.4

~/.jenv/versions » jenv remove 1.7
JDK 1.7 removed
------------------------------------------------------------
~/.jenv/versions » jenv remove 1.7.0.71
JDK 1.7.0.71 removed
------------------------------------------------------------
~/.jenv/versions » jenv remove 1.8
JDK 1.8 removed
------------------------------------------------------------
~/.jenv/versions » jenv remove 1.8.0.161
JDK 1.8.0.161 removed
------------------------------------------------------------
~/.jenv/versions » jenv remove 9.0
JDK 9.0 removed
------------------------------------------------------------
~/.jenv/versions » jenv remove 9.0.4
JDK 9.0.4 removed
------------------------------------------------------------
~/.jenv/versions » jenv versions
* system (set by /Users/rollenholt/.jenv/version)
  oracle64-1.7.0.71
  oracle64-1.8.0.161
  oracle64-9.0.4

------------------------------------------------------------
~/.jenv/versions » java -version
java version "9.0.4"
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)
------------------------------------------------------------
~/.jenv/versions » jenv local oracle64-1.8.0.161
------------------------------------------------------------
~/.jenv/versions » java -version
java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)
本文版权归作者所有,禁止一切形式的转载,复制等操作
赞赏

微信赞赏支付宝赞赏

发表评论

电子邮件地址不会被公开。 必填项已用*标注