Centos7 linux 卸载自带安装的jdk 并yum自动安装jdk1.8

时间:2022-05-19 15:09:37


一:卸载系统自带安装的JDK

注:本文参考了《 使用CentOS7卸载自带jdk安装自己的JDK1.8

通过xshell工具成功连接安装好的虚拟机之后可通过    rpm -qa | grep Java  命令来查询出系统自带的jdk(带箭头的四个就是系统自带的)注:不带箭头的那三个不要删掉

Centos7 linux 卸载自带安装的jdk 并yum自动安装jdk1.8

然后通过    rpm -e --nodeps   后面跟系统自带的jdk名    这个命令来删除系统自带的jdk,

例如:rpm -e --nodeps java-1.8.0-openjdk-1.8.0.102-4.b14.el7.x86_64
           rpm -e --nodeps java-1.8.0-openjdk-headless-1.8.0.102-4.b14.el7.x86_64
           rpm -e --nodeps java-1.7.0-openjdk-headless-1.7.0.111-2.6.7.8.el7.x86_64
           rpm -e --nodeps java-1.7.0-openjdk-1.7.0.111-2.6.7.8.el7.x86_64


Centos7 linux 卸载自带安装的jdk 并yum自动安装jdk1.8

删完之后可以再通过    rpm -qa | grep Java  命令来查询出是否删除掉

Centos7 linux 卸载自带安装的jdk 并yum自动安装jdk1.8




二:Centos7 通过yum命令安装jdk1.8

注:本段内容参考了《Centos7 通过yum命令安装jdk1.8

先查看系统有没有自带的openjdk

rpm -qa |grep java rpm -qa |grep jdk rpm -qa |grep gcj


[root@localhost java]# rpm -qa | grep java
tzdata-java-2016g-2.el7.noarch
python-javapackages-3.4.1-11.el7.noarch
[root@localhost java]# rpm -qa |grep jdk
copy-jdk-configs-1.2-1.el7.noarch
[root@localhost java]# rpm -qa |grep gcj
[root@localhost java]# 

如果安装了,首先卸载

批量卸载

rpm -qa | grep java | xargs rpm -e –nodeps
Centos7 linux 卸载自带安装的jdk 并yum自动安装jdk1.8

检索yum中有没有java1.8的包

yum list java-1.8* 

    Centos7 linux 卸载自带安装的jdk 并yum自动安装jdk1.8

    开始安装

    yum install java-1.8.0-openjdk* -y


      [root@localhost java]# yum install java-1.8.0-openjdk* -y
      Loaded plugins: fastestmirror, langpacks
      file://yum/mnt/repodata/repomd.xml: [Errno 14] curl#37 - "Couldn't open file /mnt/repodata/repomd.xml"
      Trying other mirror.
      Loading mirror speeds from cached hostfile
      Resolving Dependencies
      --> Running transaction check
      ---> Package java-1.8.0-openjdk.x86_64 1:1.8.0.102-4.b14.el7 will be installed
      ---> Package java-1.8.0-openjdk-accessibility.x86_64 1:1.8.0.102-4.b14.el7 will be installed
      --> Processing Dependency: java-atk-wrapper for package: 1:java-1.8.0-openjdk-accessibility-1.8.0.102-4.b14.el7.x86_64
      ---> Package java-1.8.0-openjdk-accessibility-debug.x86_64 1:1.8.0.102-4.b14.el7 will be installed
      ---> Package java-1.8.0-openjdk-debug.x86_64 1:1.8.0.102-4.b14.el7 will be installed
      ---> Package java-1.8.0-openjdk-demo.x86_64 1:1.8.0.102-4.b14.el7 will be installed
      ---> Package java-1.8.0-openjdk-demo-debug.x86_64 1:1.8.0.102-4.b14.el7 will be installed
      ---> Package java-1.8.0-openjdk-devel.x86_64 1:1.8.0.102-4.b14.el7 will be installed
      ---> Package java-1.8.0-openjdk-devel-debug.x86_64 1:1.8.0.102-4.b14.el7 will be installed
      ---> Package java-1.8.0-openjdk-headless.x86_64 1:1.8.0.102-4.b14.el7 will be installed
      --> Processing Dependency: jpackage-utils for package: 1:java-1.8.0-openjdk-headless-1.8.0.102-4.b14.el7.x86_64
      ---> Package java-1.8.0-openjdk-headless-debug.x86_64 1:1.8.0.102-4.b14.el7 will be installed
      ---> Package java-1.8.0-openjdk-javadoc.noarch 1:1.8.0.102-4.b14.el7 will be installed
      ---> Package java-1.8.0-openjdk-javadoc-debug.noarch 1:1.8.0.102-4.b14.el7 will be installed
      ---> Package java-1.8.0-openjdk-javadoc-zip.noarch 1:1.8.0.102-4.b14.el7 will be installed
      ---> Package java-1.8.0-openjdk-javadoc-zip-debug.noarch 1:1.8.0.102-4.b14.el7 will be installed
      ---> Package java-1.8.0-openjdk-src.x86_64 1:1.8.0.102-4.b14.el7 will be installed
      ---> Package java-1.8.0-openjdk-src-debug.x86_64 1:1.8.0.102-4.b14.el7 will be installed
      --> Running transaction check
      ---> Package java-atk-wrapper.x86_64 0:0.30.4-5.el7 will be installed
      ---> Package javapackages-tools.noarch 0:3.4.1-11.el7 will be installed
      --> Finished Dependency Resolution
      
      Dependencies Resolved
      
      ============================================================================================================================================================================
       Package                                                      Arch                         Version                                     Repository                      Size
      ============================================================================================================================================================================
      Installing:
       java-1.8.0-openjdk                                           x86_64                       1:1.8.0.102-4.b14.el7                       c7-media                       230 k
       java-1.8.0-openjdk-accessibility                             x86_64                       1:1.8.0.102-4.b14.el7                       c7-media                        38 k
       java-1.8.0-openjdk-accessibility-debug                       x86_64                       1:1.8.0.102-4.b14.el7                       c7-media                        38 k
       java-1.8.0-openjdk-debug                                     x86_64                       1:1.8.0.102-4.b14.el7                       c7-media                       234 k
       java-1.8.0-openjdk-demo                                      x86_64                       1:1.8.0.102-4.b14.el7                       c7-media                       1.9 M
       java-1.8.0-openjdk-demo-debug                                x86_64                       1:1.8.0.102-4.b14.el7                       c7-media                       2.0 M
       java-1.8.0-openjdk-devel                                     x86_64                       1:1.8.0.102-4.b14.el7                       c7-media                       9.7 M
       java-1.8.0-openjdk-devel-debug                               x86_64                       1:1.8.0.102-4.b14.el7                       c7-media                       9.7 M
       java-1.8.0-openjdk-headless                                  x86_64                       1:1.8.0.102-4.b14.el7                       c7-media                        31 M
       java-1.8.0-openjdk-headless-debug                            x86_64                       1:1.8.0.102-4.b14.el7                       c7-media                        32 M
       java-1.8.0-openjdk-javadoc                                   noarch                       1:1.8.0.102-4.b14.el7                       c7-media                        15 M
       java-1.8.0-openjdk-javadoc-debug                             noarch                       1:1.8.0.102-4.b14.el7                       c7-media                        15 M
       java-1.8.0-openjdk-javadoc-zip                               noarch                       1:1.8.0.102-4.b14.el7                       c7-media                        41 M
       java-1.8.0-openjdk-javadoc-zip-debug                         noarch                       1:1.8.0.102-4.b14.el7                       c7-media                        41 M
       java-1.8.0-openjdk-src                                       x86_64                       1:1.8.0.102-4.b14.el7                       c7-media                        45 M
       java-1.8.0-openjdk-src-debug                                 x86_64                       1:1.8.0.102-4.b14.el7                       c7-media                        45 M
      Installing for dependencies:
       java-atk-wrapper                                             x86_64                       0.30.4-5.el7                                c7-media                        71 k
       javapackages-tools                                           noarch                       3.4.1-11.el7                                c7-media                        73 k
      
      Transaction Summary
      ============================================================================================================================================================================
      Install  16 Packages (+2 Dependent packages)
      
      Total download size: 290 M
      Installed size: 1.0 G
      Downloading packages:
      ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      Total                                                                                                                                        71 MB/s | 290 MB  00:00:04
      Running transaction check
      Running transaction test
      Transaction test succeeded
      Running transaction
      Warning: RPMDB altered outside of yum.
      ** Found 14 pre-existing rpmdb problem(s), 'yum check' output follows:
      icedtea-web-1.6.2-4.el7.x86_64 has missing requires of java-1.8.0-openjdk
      icedtea-web-1.6.2-4.el7.x86_64 has missing requires of jpackage-utils
      icedtea-web-1.6.2-4.el7.x86_64 has missing requires of jpackage-utils
      ipa-client-4.4.0-12.el7.centos.x86_64 has installed conflicts freeipa-client: ipa-client-4.4.0-12.el7.centos.x86_64
      ipa-client-common-4.4.0-12.el7.centos.noarch has installed conflicts freeipa-client-common: ipa-client-common-4.4.0-12.el7.centos.noarch
      ipa-common-4.4.0-12.el7.centos.noarch has installed conflicts freeipa-common: ipa-common-4.4.0-12.el7.centos.noarch
      jline-1.0-8.el7.noarch has missing requires of java >= ('0', '1.5', None)
      jline-1.0-8.el7.noarch has missing requires of jpackage-utils
      1:libreoffice-core-5.0.6.2-3.el7.x86_64 has missing requires of java-headless >= ('1', '1.6', None)
      1:libreoffice-ure-5.0.6.2-3.el7.x86_64 has missing requires of libjvm.so()(64bit)
      rhino-1.7R4-5.el7.noarch has missing requires of jpackage-utils
      rhino-1.7R4-5.el7.noarch has missing requires of jpackage-utils
      tagsoup-1.2.1-8.el7.noarch has missing requires of jpackage-utils
      tagsoup-1.2.1-8.el7.noarch has missing requires of jpackage-utils >= ('0', '1.6', None)
        Installing : javapackages-tools-3.4.1-11.el7.noarch                                                                                                                  1/18
        Installing : 1:java-1.8.0-openjdk-headless-debug-1.8.0.102-4.b14.el7.x86_64                                                                                          2/18
        Installing : 1:java-1.8.0-openjdk-headless-1.8.0.102-4.b14.el7.x86_64                                                                                                3/18
        Installing : 1:java-1.8.0-openjdk-devel-1.8.0.102-4.b14.el7.x86_64                                                                                                   4/18
        Installing : 1:java-1.8.0-openjdk-debug-1.8.0.102-4.b14.el7.x86_64                                                                                                   5/18
        Installing : 1:java-1.8.0-openjdk-1.8.0.102-4.b14.el7.x86_64                                                                                                         6/18
        Installing : java-atk-wrapper-0.30.4-5.el7.x86_64                                                                                                                    7/18
        Installing : 1:java-1.8.0-openjdk-accessibility-debug-1.8.0.102-4.b14.el7.x86_64                                                                                     8/18
        Installing : 1:java-1.8.0-openjdk-accessibility-1.8.0.102-4.b14.el7.x86_64                                                                                           9/18
        Installing : 1:java-1.8.0-openjdk-demo-1.8.0.102-4.b14.el7.x86_64                                                                                                   10/18
        Installing : 1:java-1.8.0-openjdk-devel-debug-1.8.0.102-4.b14.el7.x86_64                                                                                            11/18
        Installing : 1:java-1.8.0-openjdk-demo-debug-1.8.0.102-4.b14.el7.x86_64                                                                                             12/18
        Installing : 1:java-1.8.0-openjdk-src-1.8.0.102-4.b14.el7.x86_64                                                                                                    13/18
        Installing : 1:java-1.8.0-openjdk-javadoc-1.8.0.102-4.b14.el7.noarch                                                                                                14/18
        Installing : 1:java-1.8.0-openjdk-javadoc-zip-1.8.0.102-4.b14.el7.noarch                                                                                            15/18
        Installing : 1:java-1.8.0-openjdk-src-debug-1.8.0.102-4.b14.el7.x86_64                                                                                              16/18
        Installing : 1:java-1.8.0-openjdk-javadoc-zip-debug-1.8.0.102-4.b14.el7.noarch                                                                                      17/18
        Installing : 1:java-1.8.0-openjdk-javadoc-debug-1.8.0.102-4.b14.el7.noarch                                                                                          18/18
        Verifying  : 1:java-1.8.0-openjdk-accessibility-debug-1.8.0.102-4.b14.el7.x86_64                                                                                     1/18
        Verifying  : java-atk-wrapper-0.30.4-5.el7.x86_64                                                                                                                    2/18
        Verifying  : 1:java-1.8.0-openjdk-accessibility-1.8.0.102-4.b14.el7.x86_64                                                                                           3/18
        Verifying  : 1:java-1.8.0-openjdk-debug-1.8.0.102-4.b14.el7.x86_64                                                                                                   4/18
        Verifying  : 1:java-1.8.0-openjdk-devel-1.8.0.102-4.b14.el7.x86_64                                                                                                   5/18
        Verifying  : 1:java-1.8.0-openjdk-headless-debug-1.8.0.102-4.b14.el7.x86_64                                                                                          6/18
        Verifying  : 1:java-1.8.0-openjdk-src-debug-1.8.0.102-4.b14.el7.x86_64                                                                                               7/18
        Verifying  : 1:java-1.8.0-openjdk-demo-1.8.0.102-4.b14.el7.x86_64                                                                                                    8/18
        Verifying  : 1:java-1.8.0-openjdk-devel-debug-1.8.0.102-4.b14.el7.x86_64                                                                                             9/18
        Verifying  : 1:java-1.8.0-openjdk-src-1.8.0.102-4.b14.el7.x86_64                                                                                                    10/18
        Verifying  : 1:java-1.8.0-openjdk-javadoc-zip-debug-1.8.0.102-4.b14.el7.noarch                                                                                      11/18
        Verifying  : 1:java-1.8.0-openjdk-demo-debug-1.8.0.102-4.b14.el7.x86_64                                                                                             12/18
        Verifying  : 1:java-1.8.0-openjdk-javadoc-1.8.0.102-4.b14.el7.noarch                                                                                                13/18
        Verifying  : javapackages-tools-3.4.1-11.el7.noarch                                                                                                                 14/18
        Verifying  : 1:java-1.8.0-openjdk-javadoc-zip-1.8.0.102-4.b14.el7.noarch                                                                                            15/18
        Verifying  : 1:java-1.8.0-openjdk-javadoc-debug-1.8.0.102-4.b14.el7.noarch                                                                                          16/18
        Verifying  : 1:java-1.8.0-openjdk-headless-1.8.0.102-4.b14.el7.x86_64                                                                                               17/18
        Verifying  : 1:java-1.8.0-openjdk-1.8.0.102-4.b14.el7.x86_64                                                                                                        18/18
      
      Installed:
        java-1.8.0-openjdk.x86_64 1:1.8.0.102-4.b14.el7                                       java-1.8.0-openjdk-accessibility.x86_64 1:1.8.0.102-4.b14.el7
        java-1.8.0-openjdk-accessibility-debug.x86_64 1:1.8.0.102-4.b14.el7                   java-1.8.0-openjdk-debug.x86_64 1:1.8.0.102-4.b14.el7
        java-1.8.0-openjdk-demo.x86_64 1:1.8.0.102-4.b14.el7                                  java-1.8.0-openjdk-demo-debug.x86_64 1:1.8.0.102-4.b14.el7
        java-1.8.0-openjdk-devel.x86_64 1:1.8.0.102-4.b14.el7                                 java-1.8.0-openjdk-devel-debug.x86_64 1:1.8.0.102-4.b14.el7
        java-1.8.0-openjdk-headless.x86_64 1:1.8.0.102-4.b14.el7                              java-1.8.0-openjdk-headless-debug.x86_64 1:1.8.0.102-4.b14.el7
        java-1.8.0-openjdk-javadoc.noarch 1:1.8.0.102-4.b14.el7                               java-1.8.0-openjdk-javadoc-debug.noarch 1:1.8.0.102-4.b14.el7
        java-1.8.0-openjdk-javadoc-zip.noarch 1:1.8.0.102-4.b14.el7                           java-1.8.0-openjdk-javadoc-zip-debug.noarch 1:1.8.0.102-4.b14.el7
        java-1.8.0-openjdk-src.x86_64 1:1.8.0.102-4.b14.el7                                   java-1.8.0-openjdk-src-debug.x86_64 1:1.8.0.102-4.b14.el7
      
      Dependency Installed:
        java-atk-wrapper.x86_64 0:0.30.4-5.el7                                              javapackages-tools.noarch 0:3.4.1-11.el7
      
      Complete!
      [root@localhost java]# 

      验证是否安装成功
      查看jdk版本

      java -version


        返回的信息

        [root@localhost java]# java -version
        openjdk version "1.8.0_102"
        OpenJDK Runtime Environment (build 1.8.0_102-b14)
        OpenJDK 64-Bit Server VM (build 25.102-b14, mixed mode)
        [root@localhost java]# 

        安装成功!!!

        此时,如果使用javac,java都是可以的。我们还没有设置环境变量,为什么直接可以使用了呢?

        这就体现出了在线安装的好处了,它会自动配置环境变量,我们通过echo $PATH命令查看

        echo $PATH
        [root@localhost java]# echo $PATH
        /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
        [root@localhost java]# 

        “:”为Linux系统中环境变量分割符。

        如何查看jdk的JAVA_HOME?

        [root@localhost java]# whereis javac
        javac: /usr/bin/javac /usr/share/man/man1/javac.1.gz
        [root@localhost java]# 


        [root@localhost java]# whereis javac
        javac: /usr/bin/javac /usr/share/man/man1/javac.1.gz
        [root@localhost java]# ls -al /usr/bin/javac
        lrwxrwxrwx. 1 root root 23 Jul 19 00:59 /usr/bin/javac -> /etc/alternatives/javac
        [root@localhost java]# ls -al /etc/alternatives/javac
        lrwxrwxrwx. 1 root root 68 Jul 19 00:59 /etc/alternatives/javac -> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.102-4.b14.el7.x86_64/bin/javac
        [root@localhost java]# ls -al /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.102-4.b14.el7.x86_64/bin/javac
        -rwxr-xr-x. 1 root root 7384 Nov 12  2016 /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.102-4.b14.el7.x86_64/bin/javac
        [root@localhost java]# 

        此时说明
        /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.102-1.b14.el7_2.x86_64/bin/javac
        是一个真实的地址。

        所以
        /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.102-1.b14.el7_2.x86_64/
        就是JAVA_HOME


        将JAVA_HOME加入环境变量


        可以添加到rc文件,比如 /etc/rc.d/rc.local
        1.使用vim (不再介绍)
        或者
        2.使用 echo >> 命令

        jhometip='# add JAVA_HOME' jhomescript='export JAVA_HOME=/etc/alternatives/java_sdk_1.8.0' sudo echo $jhometip >> /etc/rc.d/rc.local sudo echo $jhomescript >> /etc/rc.d/rc.local


          /etc/alternatives/java_sdk_1.8.0 是一个软链接,直接指向JAVA_HOME地址。

          Centos7 linux 卸载自带安装的jdk 并yum自动安装jdk1.8

          Centos7 linux 卸载自带安装的jdk 并yum自动安装jdk1.8