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
10
~$ /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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
~$ sdk list java
================================================================================
Available Java Versions
================================================================================
Vendor | Use | Version | Dist | Status | Identifier
--------------------------------------------------------------------------------
AdoptOpenJDK | | 13.0.1.j9 | adpt | | 13.0.1.j9-adpt
| | 13.0.1.hs | adpt | | 13.0.1.hs-adpt
| | 12.0.2.j9 | adpt | | 12.0.2.j9-adpt
| | 12.0.2.hs | adpt | | 12.0.2.hs-adpt
| | 11.0.5.j9 | adpt | | 11.0.5.j9-adpt
| | 11.0.5.hs | adpt | | 11.0.5.hs-adpt
| | 8.0.232.j9 | adpt | | 8.0.232.j9-adpt
| | 8.0.232.hs | adpt | | 8.0.232.hs-adpt
Amazon | | 11.0.5 | amzn | | 11.0.5-amzn
| | 8.0.232 | amzn | | 8.0.232-amzn
| >>> | 8.0.202 | amzn | installed | 8.0.202-amzn
Azul Zulu | | 13.0.1 | zulu | | 13.0.1-zulu
| | 12.0.2 | zulu | | 12.0.2-zulu
| | 11.0.5 | zulu | | 11.0.5-zulu
| | 10.0.2 | zulu | | 10.0.2-zulu
| | 9.0.7 | zulu | | 9.0.7-zulu
| | 8.0.232 | zulu | | 8.0.232-zulu
| | 8.0.202 | zulu | | 8.0.202-zulu
| | 7.0.181 | zulu | | 7.0.181-zulu
Azul ZuluFX | | 11.0.2 | zulufx | | 11.0.2-zulufx
| | 8.0.202 | zulufx | | 8.0.202-zulufx
BellSoft | | 13.0.1 | librca | | 13.0.1-librca
| | 12.0.2 | librca | | 12.0.2-librca
| | 11.0.5 | librca | | 11.0.5-librca
| | 8.0.232 | librca | | 8.0.232-librca
GraalVM | | 19.3.0.r11 | grl | | 19.3.0.r11-grl
| | 19.3.0.r8 | grl | | 19.3.0.r8-grl
| | 19.2.1 | grl | | 19.2.1-grl
| | 19.1.1 | grl | | 19.1.1-grl
| | 19.0.2 | grl | | 19.0.2-grl
| | 1.0.0 | grl | | 1.0.0-rc-16-grl
Java.net | | 14.ea.24 | open | | 14.ea.24-open
| | 13.0.1 | open | | 13.0.1-open
| | 12.0.2 | open | | 12.0.2-open
| | 11.0.2 | open | | 11.0.2-open
| | 10.0.2 | open | | 10.0.2-open
| | 9.0.4 | open | | 9.0.4-open
SAP | | 12.0.2 | sapmchn | | 12.0.2-sapmchn
| | 11.0.4 | sapmchn | | 11.0.4-sapmchn
================================================================================
Use the Identifier for installation:

$ sdk install java 11.0.3.hs-adpt
================================================================================

安裝 GraalVM

GraalVM 是 Oracle 的實驗性 Universal GraalVM,他的口號為 Run Programs Fast Anywhere 宣稱可以打通不同語言的,讓開發者混合多種語言開發。目前支援: Java、Python、Ruby、R、Scala、Kotlin、C、C++。

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 install java 19.3.0.r11-grl

Downloading: java 19.3.0.r11-grl

In progress...

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

Repackaging Java 19.3.0.r11-grl...

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

Installing: java 19.3.0.r11-grl
Done installing!

Do you want java 19.3.0.r11-grl to be set as default? (Y/n): y

Setting java 19.3.0.r11-grl as default.

~$ java -version
openjdk version "11.0.5" 2019-10-15
OpenJDK Runtime Environment (build 11.0.5+10-jvmci-19.3-b05-LTS)
OpenJDK 64-Bit GraalVM CE 19.3.0 (build 11.0.5+10-jvmci-19.3-b05-LTS, mixed mode, sharing)

安裝 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

更換 JDK

1
2
3
4
5
# 已經安裝的會標示 'installed'
sdk list java

## 更改為 GraalVM
sdk default java 19.3.0.r11-grl

安裝 gradle or maven

除了安裝 JDK 之外,也可以安裝其他工具,例如 gradle / 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

  • 全站索引
  • 學習法則
  • 思考本質
  • 一些領悟
  • 分類哲學
  • ▲ TOP ▲