java多版本管理

背景

  1. java版本的升级也比较频繁, 每年一个版本或更多
  2. 虽然java环境变量的配置无技术性可言, 但对于频繁切换也是比较枯燥的

java版本管理工具

  1. sdkman: https://sdkman.io/
  2. jenv
  3. Jabba

sdkman的安装--mac

安装

curl -s "https://get.sdkman.io" | bash

立即生效

source "$HOME/.sdkman/bin/sdkman-init.sh"

测试

sdk version

结果

$ sdk version
==== BROADCAST =================================================================
* 2020-09-02: jbang 0.43.0 @jbangdev https://git.io/JUqfJ
* 2020-09-02: jbang 0.42.1 @jbangdev https://git.io/JUqfJ
* 2020-08-25: Gradle 6.6.1 released on SDKMAN! #gradle
================================================================================

SDKMAN 5.9.0+555

sdkman的使用--mac

查看帮助

$ sdk help

Usage: sdk  [candidate] [version]
       sdk offline 

   commands:
       install   or i     [version] [local-path]
       uninstall or rm    
       list      or ls   [candidate]
       use       or u     
       default   or d     [version]
       home      or h     
       env       or e    [init]
       current   or c    [candidate]
       upgrade   or ug   [candidate]
       version   or v
       broadcast or b
       help
       offline           [enable|disable]
       selfupdate        [force]
       update
       flush             

   candidate  :  the SDK to install: groovy, scala, grails, gradle, kotlin, etc.
                 use list command for comprehensive list of candidates
                 eg: $ sdk list
   version    :  where optional, defaults to latest stable if not provided
                 eg: $ sdk install groovy
   local-path :  optional path to an existing local installation
                 eg: $ sdk install groovy 2.4.13-local /opt/groovy-2.4.13

查看正在使用的java版本

$ sdk current java

Not using any version of java

查看所有的java版本

$ sdk list java
================================================================================
Available Java Versions
================================================================================
 Vendor        | Use | Version      | Dist    | Status     | Identifier
--------------------------------------------------------------------------------
 AdoptOpenJDK  |     | 14.0.2.j9    | adpt    |            | 14.0.2.j9-adpt
               |     | 14.0.2.hs    | adpt    |            | 14.0.2.hs-adpt
               |     | 13.0.2.j9    | adpt    |            | 13.0.2.j9-adpt
               |     | 13.0.2.hs    | adpt    |            | 13.0.2.hs-adpt
               |     | 12.0.2.j9    | adpt    |            | 12.0.2.j9-adpt
               |     | 12.0.2.hs    | adpt    |            | 12.0.2.hs-adpt
               |     | 11.0.8.j9    | adpt    |            | 11.0.8.j9-adpt
               |     | 11.0.8.hs    | adpt    |            | 11.0.8.hs-adpt
               |     | 8.0.265.j9   | adpt    |            | 8.0.265.j9-adpt
               |     | 8.0.265.hs   | adpt    |            | 8.0.265.hs-adpt
 Amazon        |     | 11.0.8       | amzn    |            | 11.0.8-amzn
               |     | 8.0.265      | amzn    |            | 8.0.265-amzn
 Azul Zulu     |     | 14.0.2       | zulu    |            | 14.0.2-zulu
               |     | 13.0.4       | zulu    |            | 13.0.4-zulu
               |     | 13.0.4.fx    | zulu    |            | 13.0.4.fx-zulu
               |     | 12.0.2       | zulu    |            | 12.0.2-zulu
               |     | 11.0.8       | zulu    |            | 11.0.8-zulu
               |     | 11.0.8.fx    | zulu    |            | 11.0.8.fx-zulu
               |     | 10.0.2       | zulu    |            | 10.0.2-zulu
               |     | 9.0.7        | zulu    |            | 9.0.7-zulu
               |     | 8.0.265      | zulu    |            | 8.0.265-zulu
               |     | 8.0.265.fx   | zulu    |            | 8.0.265.fx-zulu
               |     | 8.0.232.fx   | zulu    |            | 8.0.232.fx-zulu
               |     | 8.0.202      | zulu    |            | 8.0.202-zulu
               |     | 7.0.262      | zulu    |            | 7.0.262-zulu
               |     | 7.0.181      | zulu    |            | 7.0.181-zulu
 BellSoft      |     | 14.0.2.fx    | librca  |            | 14.0.2.fx-librca
               |     | 14.0.2       | librca  |            | 14.0.2-librca
               |     | 13.0.2.fx    | librca  |            | 13.0.2.fx-librca
               |     | 13.0.2       | librca  |            | 13.0.2-librca
               |     | 12.0.2       | librca  |            | 12.0.2-librca
               |     | 11.0.8.fx    | librca  |            | 11.0.8.fx-librca
               |     | 11.0.8       | librca  |            | 11.0.8-librca
               |     | 8.0.265.fx   | librca  |            | 8.0.265.fx-librca
               |     | 8.0.265      | librca  |            | 8.0.265-librca
 GraalVM       |     | 20.2.0.r11   | grl     |            | 20.2.0.r11-grl
               |     | 20.2.0.r8    | grl     |            | 20.2.0.r8-grl
               |     | 20.1.0.r11   | grl     |            | 20.1.0.r11-grl
               |     | 20.1.0.r8    | grl     |            | 20.1.0.r8-grl
               |     | 20.0.0.r11   | grl     |            | 20.0.0.r11-grl
               |     | 20.0.0.r8    | grl     |            | 20.0.0.r8-grl
               |     | 19.3.1.r11   | grl     |            | 19.3.1.r11-grl
               |     | 19.3.1.r8    | grl     |            | 19.3.1.r8-grl
 Java.net      |     | 16.ea.13     | open    |            | 16.ea.13-open
               |     | 15.ea.36     | open    |            | 15.ea.36-open
               |     | 14.0.2       | open    |            | 14.0.2-open
               |     | 13.0.2       | open    |            | 13.0.2-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           |     | 14.0.2       | sapmchn |            | 14.0.2-sapmchn
               |     | 13.0.2       | sapmchn |            | 13.0.2-sapmchn
               |     | 12.0.2       | sapmchn |            | 12.0.2-sapmchn
               |     | 11.0.8       | sapmchn |            | 11.0.8-sapmchn
================================================================================
Use the Identifier for installation:

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

安装指定的java版本

安装jdk1.8, 版本号填写的是上图中的Identifier列

$ sdk install java 8.0.265.hs-adpt

Downloading: java 8.0.265.hs-adpt

In progress...

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

Repackaging Java 8.0.265.hs-adpt...

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

Installing: java 8.0.265.hs-adpt
Done installing!


Setting java 8.0.265.hs-adpt as default.

同样的方式安装java 14.0.2.j9-adpt

再次查看有的java版本

$ sdk list java
================================================================================
Available Java Versions
================================================================================
 Vendor        | Use | Version      | Dist    | Status     | Identifier
--------------------------------------------------------------------------------
 AdoptOpenJDK  |     | 14.0.2.j9    | adpt    | installed  | 14.0.2.j9-adpt
               |     | 14.0.2.hs    | adpt    |            | 14.0.2.hs-adpt
               |     | 13.0.2.j9    | adpt    |            | 13.0.2.j9-adpt
               |     | 13.0.2.hs    | adpt    |            | 13.0.2.hs-adpt
               |     | 12.0.2.j9    | adpt    |            | 12.0.2.j9-adpt
               |     | 12.0.2.hs    | adpt    |            | 12.0.2.hs-adpt
               |     | 11.0.8.j9    | adpt    |            | 11.0.8.j9-adpt
               |     | 11.0.8.hs    | adpt    |            | 11.0.8.hs-adpt
               |     | 8.0.265.j9   | adpt    |            | 8.0.265.j9-adpt
               | >>> | 8.0.265.hs   | adpt    | installed  | 8.0.265.hs-adpt
  1. 已经安装的会显示installed
  2. 正在使用的会显示>>>

切换java的版本

$ sdk use java 8.0.265.hs-adpt

Using java version 8.0.265.hs-adpt in this shell.

查看java的版本

$ java -version
openjdk version "1.8.0_265"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_265-b01)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.265-b01, mixed mode)
  1. 切换版本仅在当前shell有效, 新打开的终端会使用默认的java版本
  2. 可以通过设置默认版本, 使新终端也是相同的java版本

设置默认版本

$ sdk default java 8.0.265.hs-adpt

Default java version set to 8.0.265.hs-adpt

TODO 另外两种java版本管理调研

你可能感兴趣的:(java多版本管理)