转载

Java Version Manager

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

底下整理的都是針對 macOS。

內建工具

java_home

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

~$ /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

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…

安裝:

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

確認:

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 可以用

~$ 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

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.

驗證:

~$ 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

~$ 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
原文  https://rickhw.github.io/2019/04/07/Coding/Java-Version-Manager/
正文到此结束
Loading...