摘要:这里主要讲解在远程连接服务器后,Centos7的命令自动安装java环境

1.查询可以在线安装的java版本【yum -y list java*】

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
[root@iZ4qq76j5p04zsZ ~]# yum -y list java*
Loaded plugins: fastestmirror
Determining fastest mirrors
base | 3.6 kB 00:00:00
epel | 5.3 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/7): base/7/x86_64/group_gz | 165 kB 00:00:00
(2/7): epel/x86_64/group_gz | 90 kB 00:00:00
(3/7): epel/x86_64/updateinfo | 1.0 MB 00:00:00
(4/7): base/7/x86_64/primary_db | 6.0 MB 00:00:00
(5/7): extras/7/x86_64/primary_db | 152 kB 00:00:00
(6/7): epel/x86_64/primary_db | 6.9 MB 00:00:00
(7/7): updates/7/x86_64/primary_db | 2.8 MB 00:00:00
Available Packages
java-1.6.0-openjdk.x86_64 1:1.6.0.41-1.13.13.1.el7_3 base
java-1.6.0-openjdk-demo.x86_64 1:1.6.0.41-1.13.13.1.el7_3 base
java-1.6.0-openjdk-devel.x86_64 1:1.6.0.41-1.13.13.1.el7_3 base
java-1.6.0-openjdk-javadoc.x86_64 1:1.6.0.41-1.13.13.1.el7_3 base
java-1.6.0-openjdk-src.x86_64 1:1.6.0.41-1.13.13.1.el7_3 base
java-1.7.0-openjdk.x86_64 1:1.7.0.231-2.6.19.2.el7_7 updates
java-1.7.0-openjdk-accessibility.x86_64 1:1.7.0.231-2.6.19.2.el7_7 updates
java-1.7.0-openjdk-demo.x86_64 1:1.7.0.231-2.6.19.2.el7_7 updates
java-1.7.0-openjdk-devel.x86_64 1:1.7.0.231-2.6.19.2.el7_7 updates
java-1.7.0-openjdk-headless.x86_64 1:1.7.0.231-2.6.19.2.el7_7 updates
java-1.7.0-openjdk-javadoc.noarch 1:1.7.0.231-2.6.19.2.el7_7 updates
java-1.7.0-openjdk-src.x86_64 1:1.7.0.231-2.6.19.2.el7_7 updates
java-1.8.0-openjdk.i686 1:1.8.0.222.b10-1.el7_7 updates
java-1.8.0-openjdk.x86_64 1:1.8.0.222.b10-1.el7_7 updates
java-1.8.0-openjdk-accessibility.i686 1:1.8.0.222.b10-1.el7_7 updates
java-1.8.0-openjdk-accessibility.x86_64 1:1.8.0.222.b10-1.el7_7 updates
java-1.8.0-openjdk-accessibility-debug.i686 1:1.8.0.222.b10-1.el7_7 updates
java-1.8.0-openjdk-accessibility-debug.x86_64

后面的就省略了

2.选择自己需要的版本进行安装(我选择java 1.8的版本)【yum -y install java-1.8.0-openjdk.x86_64】

最后会显示【Complete!】代表成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@iZ4qq76j5p04zsZ ~]# yum -y install java-1.8.0-openjdk.x86_64
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package java-1.8.0-openjdk.x86_64 1:1.8.0.222.b10-1.el7_7 will be installed
--> Processing Dependency: java-1.8.0-openjdk-headless(x86-64) = 1:1.8.0.222.b10-1.el7_7 for package: 1:java-1.8.0-openjdk-1.8.0.222.b10-1.el7_7.x86_64
--> Processing Dependency: xorg-x11-fonts-Type1 for package: 1:java-1.8.0-openjdk-1.8.0.222.b10-1.el7_7.x86_64
--> Processing Dependency: libjvm.so(SUNWprivate_1.1)(64bit) for package: 1:java-1.8.0-openjdk-1.8.0.222.b10-1.el7_7.x86_64
--> Processing Dependency: libjpeg.so.62(LIBJPEG_6.2)(64bit) for package: 1:java-1.8.0-openjdk-1.8.0.222.b10-1.el7_7.x86_64
--> Processing Dependency: libjava.so(SUNWprivate_1.1)(64bit) for package: 1:java-1.8.0-openjdk-1.8.0.222.b10-1.el7_7.x86_64
==================================中间省略==============================================
Dependency Installed:
atk.x86_64 0:2.28.1-1.el7 cairo.x86_64 0:1.15.12-4.el7
copy-jdk-configs.noarch 0:3.3-10.el7_5 dejavu-fonts-common.noarch 0:2.33-6.el7
dejavu-sans-fonts.noarch 0:2.33-6.el7 fontconfig.x86_64 0:2.13.0-4.3.el7
python-javapackages.noarch 0:3.4.1-11.el7 python-lxml.x86_64 0:3.2.1-4.el7
ttmkfdir.x86_64 0:3.0.9-42.el7 tzdata-java.noarch 0:2019c-1.el7
xorg-x11-font-utils.x86_64 1:7.5-21.el7 xorg-x11-fonts-Type1.noarch 0:7.5-9.el7

Complete!

3.安装好了我们要测试下是否可用【java -version】

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

4. 查看自己服务器中已经安装的java 【yum list installed |grep java】

如果运行过这个命令发现,什么都没有显示。则表示没有安装。

1
2
3
4
5
6
[root@iZ4qq76j5p04zsZ ~]# yum list installed |grep java
java-1.8.0-openjdk.x86_64 1:1.8.0.222.b10-1.el7_7 @updates
java-1.8.0-openjdk-headless.x86_64 1:1.8.0.222.b10-1.el7_7 @updates
javapackages-tools.noarch 3.4.1-11.el7 @base
python-javapackages.noarch 3.4.1-11.el7 @base
tzdata-java.noarch 2019c-1.el7 @updates

5.使用自己查询的java名字进行卸载【yum -y remove 名称】

卸载tzdata

1
[root@iZ4qq76j5p04zsZ ~]# yum -y remove tzdata-java.noarch

卸载完成后,你也可以重复上面的步骤进行更换版本等。使用yum安装的方式是自动的,环境变量等也是自动的,所以不用再去设置环境变量。