02. Install and switch different versions of JDK under Linux (through alternatives)

Introduction

This article will introduce how to install openjdk online and offline through alternatives , and switch to the appropriate version of jdk.
Alternatives facilitate unified management of all registered jdk versions.
The operation demonstration environment is CentOS system, and other operating systems such as openEuler can also be used as a reference.

The operating system version is as follows

name Version
CentOS 7.6+

1.1 Online installation

Search openjdk in yum source library

[root@k8s-node02 ~]# yum search openjdk
已加载插件:fastestmirror, langpacks
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
Determining fastest mirrors
 * elrepo: mirrors.tuna.tsinghua.edu.cn
===================================================================== N/S matched: openjdk =====================================================================
java-1.6.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.6.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.6.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.6.0-openjdk-javadoc.x86_64 : OpenJDK API Documentation
java-1.6.0-openjdk-src.x86_64 : OpenJDK Source Bundle
java-1.7.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.7.0-openjdk-accessibility.x86_64 : OpenJDK accessibility connector
java-1.7.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.7.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.7.0-openjdk-headless.x86_64 : The OpenJDK runtime environment without 

Select the version you want to install

[root@k8s-node02 ~]# yum install java-1.8.0-openjdk
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * elrepo: mirrors.tuna.tsinghua.edu.cn
base                                                                                                                                     | 3.6 kB  00:00:00
docker-ce-stable                                                                                                                         | 3.5 kB  00:00:00
elrepo                                                                                                                                   | 3.0 kB  00:00:00
extras                                                                                                                                   | 2.9 kB  00:00:00
updates                                                                                                                                  | 2.9 kB  00:00:00
(1/4): docker-ce-stable/7/x86_64/primary_db                                                                                              | 116 kB  00:00:00
(2/4): extras/7/x86_64/primary_db                                                                                                        | 250 kB  00:00:00
(3/4): updates/7/x86_64/primary_db                                                                                                       |  22 MB  00:00:11
(4/4): elrepo/x86_64/primary_db                                                                                                          | 350 kB  00:00:18
正在解决依赖关系
--> 正在检查事务

Check if the installation is successful

[root@node03 ~]# java -version
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_222-b10)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.222-b10, mixed mode)
[root@node03 ~]#
    

1.2 Offline installation

Download offline package

Download JDK from Oracle official website

Insert image description here

Unzip JDK

After downloading, upload it to the server and extract it to the /usr/lib/jvm/ directory .

[root@k8s-node02 ~]#  tar -xzvf jdk-20_linux-x64_bin.tar.gz -C /usr/lib/jvm
jdk-20.0.2/LICENSE
jdk-20.0.2/README
jdk-20.0.2/bin/jar
jdk-20.0.2/bin/jarsigner
jdk-20.0.2/bin/java

View the directory /usr/lib/jvm/jdk-20.0.2/bin

Insert image description here

Register JDK into alternatives

Check the installed jdk ( press Enter to exit the command and keep the current version)

[root@k8s-node02 ~]# alternatives --config java

共有 3 个提供“java”的程序。

  选项    命令
-----------------------------------------------
   1           java-1.7.0-openjdk.x86_64 (/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.261-2.6.22.2.el7_8.x86_64/jre/bin/java)
*+ 2           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.362.b08-1.el7_9.x86_64/jre/bin/java)
   3           java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.19.0.7-1.el7_9.x86_64/bin/java)

按 Enter 保留当前选项[+],或者键入选项编号:^Z

Since there are already 3 versions, we fill in 4 as the final serial number here.

[root@k8s-node02 ~]# update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-20.0.2/bin/java 4
[root@k8s-node02 ~]# update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-20.0.2/bin/javac 4

Check again, jdk20 has been registered and installed (press Enter to exit the command), but the server jdk is still using jdk1.8 and has not been switched to jdk20 yet.

[root@k8s-node02 ~]# alternatives --config java

共有 4 个提供“java”的程序。

  选项    命令
-----------------------------------------------
   1           java-1.7.0-openjdk.x86_64 (/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.261-2.6.22.2.el7_8.x86_64/jre/bin/java)
*+ 2           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.362.b08-1.el7_9.x86_64/jre/bin/java)
   3           java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.19.0.7-1.el7_9.x86_64/bin/java)
   4           /usr/lib/jvm/jdk-20.0.2/bin/java

按 Enter 保留当前选项[+],或者键入选项编号:

1.3 Manage JDK versions through alternatives

Take switching jdk20 as an example

switchjava

To toggle the version list displayed by the command, type the option number and enter 4


[root@k8s-node02 ~]# update-alternatives --config java

共有 4 个提供“java”的程序。

  选项    命令
-----------------------------------------------
   1           java-1.7.0-openjdk.x86_64 (/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.261-2.6.22.2.el7_8.x86_64/jre/bin/java)
*+ 2           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.362.b08-1.el7_9.x86_64/jre/bin/java)
   3           java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.19.0.7-1.el7_9.x86_64/bin/java)
   4           /usr/lib/jvm/jdk-20.0.2/bin/java

按 Enter 保留当前选项[+],或者键入选项编号:4
[root@k8s-node02 ~]# java -version
java version "20.0.2" 2023-07-18
Java(TM) SE Runtime Environment (build 20.0.2+9-78)
Java HotSpot(TM) 64-Bit Server VM (build 20.0.2+9-78, mixed mode, sharing)

switch javac

Switch the version list displayed by the command, enter the option number and enter 2 (because several versions do not have javac installed, so although we specified the number as 4 in step 1.2, the default sorting is still 2)


[root@k8s-node02 ~]# update-alternatives --config javac

共有 2 个提供“javac”的程序。

  选项    命令
-----------------------------------------------
   1           java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.19.0.7-1.el7_9.x86_64/bin/javac)
*+ 2           /usr/lib/jvm/jdk-20.0.2/bin/javac

按 Enter 保留当前选项[+],或者键入选项编号:2

At this point, the version switching is completed.
If you want to switch to another version, use the above two commands again and enter the corresponding number to switch.

Guess you like

Origin blog.csdn.net/ChennyWJS/article/details/131979094