Java Version Manager


現在各個語言都有 Version Manager,像是 node 的 nvm、ruby 的 rvm、golang 的 gvm,Java 沒有官方的工具,但也有類似的工具。

底下整理的都是針對 macOS。


內建工具

java_home

macOS 有內建一個工具:java_home 可以知道目前的 JAVA_HOME 在哪。

1
2
3
4
5
6
7
8
9
~$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/openjdk-11.0.1.jdk/Contents/Home

## 了解各版本的路徑
~$ /usr/libexec/java_home -V
Matching Java Virtual Machines (3):
11.0.1, x86_64: "OpenJDK 11.0.1" /Library/Java/JavaVirtualMachines/openjdk-11.0.1.jdk/Contents/Home
1.8.0, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home
1.7.0_51, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home

指定 JDK

1
2
3
4
5
6
7
8
9
10
export JAVA_7_HOME=$(/usr/libexec/java_home -v1.7)
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)

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

# default to Java 8
java8

SDKMan

SDKMan 是用來管理 JVM-base 套件的管理工具,包含 Java SDK, Groovy, Scala, Kotlin, Ant, Gradle, Maven , Sprint Boot, Vert.x…

安裝:

1
2
~$ curl -s "https://get.sdkman.io" | bash
~$ source "$HOME/.sdkman/bin/sdkman-init.sh"

確認:

1
2
3
4
5
6
7
8
sdk version
==== BROADCAST =================================================================
* 2019-04-04: Springboot 2.1.4.RELEASE released on SDKMAN! #springboot
* 2019-04-03: Springboot 2.0.9.RELEASE released on SDKMAN! #springboot
* 2019-04-03: Springboot 1.5.20.RELEASE released on SDKMAN! #springboot
================================================================================

SDKMAN 5.7.3+337

顯示哪一些 jdk 可以用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
~$ sdk list java
================================================================================
Available Java Versions
================================================================================
13.ea.15-open > * 8.0.202-amzn
12.0.0-zulu 8.0.202.j9-adpt
12.0.0-open 8.0.202.hs-adpt
11.0.2-zulufx 8.0.202-zulufx
11.0.2-sapmchn 8.0.201-oracle
11.0.2-zulu 7.0.181-zulu
11.0.2-open 1.0.0-rc-15-grl
11.0.2-amzn 1.0.0-rc-14-grl
11.0.2.j9-adpt 1.0.0-rc-13-grl
11.0.2.hs-adpt 1.0.0-rc-12-grl
10.0.2-zulu 1.0.0-rc-11-grl
10.0.2-open 1.0.0-rc-10-grl
9.0.7-zulu 1.0.0-rc-9-grl
9.0.4-open 1.0.0-rc-8-grl
8.0.202-zulu

================================================================================
+ - local version
* - installed
> - currently in use

安裝 Amazon JDK (Corretto)

安裝 Amazon - Java 8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
sdk install java 8.0.202-amzn
tr: Illegal byte sequence

Downloading: java 8.0.202-amzn

In progress...

######################################################################## 100.0%

Repackaging Java 8.0.202-amzn...

Done repackaging...
Cleaning up residual files...

Installing: java 8.0.202-amzn
Done installing!


Setting java 8.0.202-amzn as default.

驗證:

1
2
3
4
5
6
7
~$ java -version
openjdk version "1.8.0_202"
OpenJDK Runtime Environment Corretto-8.202.08.2 (build 1.8.0_202-b08)
OpenJDK 64-Bit Server VM Corretto-8.202.08.2 (build 25.202-b08, mixed mode)

echo $JAVA_HOME
/Users/rick/.sdkman/candidates/java/current

安裝 gradle or maven

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
~$ sdk install gradle
tr: Illegal byte sequence

Downloading: gradle 5.3.1

In progress...

######################################################################## 100.0%

Installing: gradle 5.3.1
Done installing!


Setting gradle 5.3.1 as default.

~$ echo $GRADLE_HOME
/Users/rick/.sdkman/candidates/gradle/current

延伸閱讀

參考資料


Comments