Home >Java >javaTutorial >Flexible switching and management of multiple java versions

Flexible switching and management of multiple java versions

angryTom
angryTomforward
2019-11-28 16:41:492995browse

Foreword: In the past two years, the Java version has been upgraded frequently, and I feel that I have just mastered Java8. When writing this article, I have heard the news of java14, whether it is to try new features (the use of the super powerful function of Collectors.teeing in Java12) or due to project For upgrade/compatibility needs, we may all face the situation of managing multiple Java versions.

Flexible switching and management of multiple java versions

In addition, Oracle has changed the user agreement since Java11, and any commercial use will be charged. While writing this article, I got the news "Microsoft announced that it will join OpenJDK". If you can't beat it, choose OpenJDK. Just G for a while, just understand it as a story

(Recommended video: java video tutorial)

Configuring a single Java environment variable itself has no technical content at all, but when multiple Java versions need to be managed, repeatedly configuring environment variables is obviously very boring. According to the traditional configuration method, we cannot flexibly switch Java versions

How to easily manage and use multiple versions of Java?

Multi-version Java management

Obviously we are not the first to have this dilemma. There are three existing solutions that I know of:

● Jabba

● jenv

● sdkman

This article mainly explains how to break the difficulties we face through sdkman and help us flexibly configure Introduction to using Java

sdkman

SDKMAN is a software development toolkit for managing multiple software (Java, Groovy, Scala, Kotlin and Ceylon. Ant, Gradle, etc.).

It provides a convenient command line interface (CLI) and API for installing, switching, removing and listing candidates. This article mainly explains the use of sdkman by managing Java

sdkman installation

It is very easy to install sdkman on a Unix-like platform. It can be successfully installed on Mac OSX, Linux, WLS, Cygwin, Solaris and FreeBSD, and also supports Bash and ZSH shells.

Just open a new terminal and enter:

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

Follow the corresponding instruction prompts, and continue to enter after completing the corresponding operations:

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

Here we can verify The installed version of sdk:

$ sdk version
sdk version

Flexible switching and management of multiple java versions

The red box mark in the above picture shows my current sdkman version. Every time I execute the sdk version command, I will check whether there is a new version. If you want to update, enter y

Some system releases do not include zip and unzip. If you encounter related errors during installation, you can enter the following command to install zip and unzip

$ sudo apt-get install zip unzip

Installation from above As can be seen from the command, the default installation path of sdkman is under $HOME/.sdkman. We can also customize the installation path by specifying the SDKMAN_DIR variable value:

$ export SDKMAN_DIR="/usr/local/sdkman" && curl -s "https://get.sdkman.io" | bash

The installation of sdkman is over here, let’s take a look at how to use it

sdkman tutorial

To learn a new thing from the command line, of course, check its help Command, enter:

$ sdkman help
sdk help[object Object]

I feel that after the above picture distinguishes the content by color, the instructions for using sdkman are over. Let’s follow the above picture to explain the usage tutorial in detail

sdk list

First enter:

$ sdk list
sdk list[object Object]

The green marks are all available candidates integrated by sdkman. By pressing the "enter" button, you will see more Available candidate

We specify the candidate, enter:

$ sdk list java
sdk list java[object Object]

From the above picture you can see all the available versions of java, as well as the identifier and status. I have installed java 12 and 11

With this information as a basis, we can install any built-in software development package of sdkman, continue to use java as an example

sdk install

Looking back at the output of the sdkman help command, using the install command, we install another Java latest 13.0.1.j9 version

Flexible switching and management of multiple java versions

As you can see from the above picture , the green marked content is the version value in the list command result, but the error is unavailable. Enter the indentifier number to download normally. It should be noted here that

After the installation is completed, the status will program the installed status

sdk current

When installing multiple versions of java, we enter the following command to get the version currently using candidate

$ sdk current java
sdk current java

Flexible switching and management of multiple java versions

sdk use

Understand the current version. If we want to switch to another version, we can enter:

$ sdk use java 12.0.2.j9-adpt

Note⚠️: The same here Is the value of the specified indentifier

Flexible switching and management of multiple java versions

sdk default

如果我们想指定某个版本为默认版本,可以输入:

$ sdk default java jdk1.8.0_162.jdk

注意⚠️: 这里同样是指定的 indentifier 的值

Flexible switching and management of multiple java versions

sdk uninstall

当我们想卸载某个版本可以输入:

$ sdk uninstall java 12.0.2.j9-adpt

注意⚠️: 这里同样是指定的 indentifier 的值

sdk upgrade

如果我们想升级某个 candidate,可以输入:

$ sdk upgrade java
sdk flush

使用 sdkman 时间变长也会慢慢产生很多缓存内容,我们可以输入

清理广播消息:

$ sdk flush broadcast

清理下载的 sdk 二进制文件(长时间使用后清理,可以节省出很多空间):

$ sdk flush archives

清理临时文件内容:

$ sdk flush temp

到这里 sdkman 的基本使用就已经介绍完了,其实这些命令都不用急,想不起来的时候执行 sdk help 来临时查看一下就好

sdkman 卸载

如果我们不喜欢 sdkman 了,我们也可以轻松的卸载掉它:

$ tar zcvf ~/sdkman-backup_$(date +%F-%kh%M).tar.gz -C ~/ .sdkman
$ rm -rf ~/.sdkman

最后打开你的 .bashrc、.bash_profile 和/或者 .profile,找到并删除下面这几行。

#THIS MUST BE AT THE END OF THE FILE FOR SDKMAN TO WORK!!!
[[ -s "/home/dudette/.sdkman/bin/sdkman-init.sh" ]] && source "/home/dudette/.sdkman/bin/sdkman-init.sh"

我用的 zshrc,找到 .zshrc 文件删除掉上面内容即可

到这里基于 Unix 系统的,有关 sdkman 的安装,使用及下载都已经介绍完了,可以上手试一试了

本文来自php中文网,java教程栏目,欢迎学习!  

The above is the detailed content of Flexible switching and management of multiple java versions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete