Centos下用yum命令按照jdk

一、查看是否已经安装了JDK

#查看本机是否已经安装了jdk
$ sudo yum list installed | grep java
java-1.8.0-openjdk.x86_64               1:1.8.0.201.b09-2.el7_6        @updates 
java-1.8.0-openjdk-accessibility.x86_64 1:1.8.0.201.b09-2.el7_6        @updates 
java-1.8.0-openjdk-accessibility-debug.x86_64
java-1.8.0-openjdk-debug.x86_64         1:1.8.0.201.b09-2.el7_6        @updates 
java-1.8.0-openjdk-demo.x86_64          1:1.8.0.201.b09-2.el7_6        @updates 
java-1.8.0-openjdk-demo-debug.x86_64    1:1.8.0.201.b09-2.el7_6        @updates 
java-1.8.0-openjdk-devel.x86_64         1:1.8.0.201.b09-2.el7_6        @updates 
java-1.8.0-openjdk-devel-debug.x86_64   1:1.8.0.201.b09-2.el7_6        @updates 
java-1.8.0-openjdk-headless.x86_64      1:1.8.0.201.b09-2.el7_6        @updates 
java-1.8.0-openjdk-headless-debug.x86_64
java-1.8.0-openjdk-javadoc.noarch       1:1.8.0.201.b09-2.el7_6        @updates 
java-1.8.0-openjdk-javadoc-debug.noarch 1:1.8.0.201.b09-2.el7_6        @updates 
java-1.8.0-openjdk-javadoc-zip.noarch   1:1.8.0.201.b09-2.el7_6        @updates 
java-1.8.0-openjdk-javadoc-zip-debug.noarch
java-1.8.0-openjdk-src.x86_64           1:1.8.0.201.b09-2.el7_6        @updates 
java-1.8.0-openjdk-src-debug.x86_64     1:1.8.0.201.b09-2.el7_6        @updates 
java-atk-wrapper.x86_64                 0.30.4-5.el7                   @base    
javapackages-tools.noarch               3.4.1-11.el7                   @base    
python-javapackages.noarch              3.4.1-11.el7                   @base    
tzdata-java.noarch                      2018i-1.el7                    @updates

二、卸载原有jdk

# *表示卸载所有以java-1.8.0-openjdk开头的,-y 表示自动回复yes
$ sudo yum -y remove java-1.8.0-openjdk* javapackages-tools.noarch python-javapackages.noarch tzdata-java.noarch

三、查看JDk软件包列表

$ sudo yum list java*
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.163.com
 * updates: mirrors.163.com
可安装的软件包
java-1.6.0-openjdk.x86_64                                       1:1.6.0.41-1.13.13.1.el7_3                    base       # jdk6
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.211-2.6.17.1.el7_6                    updates    # jdk7
java-1.7.0-openjdk-accessibility.x86_64                         1:1.7.0.211-2.6.17.1.el7_6                    updates
java-1.7.0-openjdk-demo.x86_64                                  1:1.7.0.211-2.6.17.1.el7_6                    updates
java-1.7.0-openjdk-devel.x86_64                                 1:1.7.0.211-2.6.17.1.el7_6                    updates
java-1.7.0-openjdk-headless.x86_64                              1:1.7.0.211-2.6.17.1.el7_6                    updates
java-1.7.0-openjdk-javadoc.noarch                               1:1.7.0.211-2.6.17.1.el7_6                    updates
java-1.7.0-openjdk-src.x86_64                                   1:1.7.0.211-2.6.17.1.el7_6                    updates
java-1.8.0-openjdk.i686                                         1:1.8.0.191.b12-1.el7_6                       updates    # jdk8 
java-1.8.0-openjdk.x86_64                                       1:1.8.0.201.b09-2.el7_6                       updates
java-1.8.0-openjdk-accessibility.i686                           1:1.8.0.191.b12-1.el7_6                       updates
java-1.8.0-openjdk-accessibility.x86_64                         1:1.8.0.201.b09-2.el7_6                       updates
java-1.8.0-openjdk-accessibility-debug.i686                     1:1.8.0.191.b12-1.el7_6                       updates
java-1.8.0-openjdk-accessibility-debug.x86_64                   1:1.8.0.201.b09-2.el7_6                       updates
java-1.8.0-openjdk-debug.i686                                   1:1.8.0.191.b12-1.el7_6                       updates
java-1.8.0-openjdk-debug.x86_64                                 1:1.8.0.201.b09-2.el7_6                       updates
java-1.8.0-openjdk-demo.i686                                    1:1.8.0.191.b12-1.el7_6                       updates
java-1.8.0-openjdk-demo.x86_64                                  1:1.8.0.201.b09-2.el7_6                       updates
java-1.8.0-openjdk-demo-debug.i686                              1:1.8.0.191.b12-1.el7_6                       updates
java-1.8.0-openjdk-demo-debug.x86_64                            1:1.8.0.201.b09-2.el7_6                       updates
java-1.8.0-openjdk-devel.i686                                   1:1.8.0.191.b12-1.el7_6                       updates
java-1.8.0-openjdk-devel.x86_64                                 1:1.8.0.201.b09-2.el7_6                       updates
java-1.8.0-openjdk-devel-debug.i686                             1:1.8.0.191.b12-1.el7_6                       updates
java-1.8.0-openjdk-devel-debug.x86_64                           1:1.8.0.201.b09-2.el7_6                       updates
java-1.8.0-openjdk-headless.i686                                1:1.8.0.191.b12-1.el7_6                       updates
java-1.8.0-openjdk-headless.x86_64                              1:1.8.0.201.b09-2.el7_6                       updates
java-1.8.0-openjdk-headless-debug.i686                          1:1.8.0.191.b12-1.el7_6                       updates
java-1.8.0-openjdk-headless-debug.x86_64                        1:1.8.0.201.b09-2.el7_6                       updates
java-1.8.0-openjdk-javadoc.noarch                               1:1.8.0.201.b09-2.el7_6                       updates
java-1.8.0-openjdk-javadoc-debug.noarch                         1:1.8.0.201.b09-2.el7_6                       updates
java-1.8.0-openjdk-javadoc-zip.noarch                           1:1.8.0.201.b09-2.el7_6                       updates
java-1.8.0-openjdk-javadoc-zip-debug.noarch                     1:1.8.0.201.b09-2.el7_6                       updates
java-1.8.0-openjdk-src.i686                                     1:1.8.0.191.b12-1.el7_6                       updates
java-1.8.0-openjdk-src.x86_64                                   1:1.8.0.201.b09-2.el7_6                       updates
java-1.8.0-openjdk-src-debug.i686                               1:1.8.0.191.b12-1.el7_6                       updates
java-1.8.0-openjdk-src-debug.x86_64                             1:1.8.0.201.b09-2.el7_6                       updates
java-11-openjdk.i686                                            1:11.0.2.7-0.el7_6                            updates    #jdk11
java-11-openjdk.x86_64                                          1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-debug.i686                                      1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-debug.x86_64                                    1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-demo.i686                                       1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-demo.x86_64                                     1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-demo-debug.i686                                 1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-demo-debug.x86_64                               1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-devel.i686                                      1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-devel.x86_64                                    1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-devel-debug.i686                                1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-devel-debug.x86_64                              1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-headless.i686                                   1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-headless.x86_64                                 1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-headless-debug.i686                             1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-headless-debug.x86_64                           1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-javadoc.i686                                    1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-javadoc.x86_64                                  1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-javadoc-debug.i686                              1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-javadoc-debug.x86_64                            1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-javadoc-zip.i686                                1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-javadoc-zip.x86_64                              1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-javadoc-zip-debug.i686                          1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-javadoc-zip-debug.x86_64                        1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-jmods.i686                                      1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-jmods.x86_64                                    1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-jmods-debug.i686                                1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-jmods-debug.x86_64                              1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-src.i686                                        1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-src.x86_64                                      1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-src-debug.i686                                  1:11.0.2.7-0.el7_6                            updates
java-11-openjdk-src-debug.x86_64                                1:11.0.2.7-0.el7_6                            updates
java-atk-wrapper.i686                                           0.30.4-5.el7                                  base   
java-atk-wrapper.x86_64                                         0.30.4-5.el7                                  base   
java_cup.noarch                                                 1:0.11a-16.el7                                base   
java_cup-javadoc.noarch                                         1:0.11a-16.el7                                base   
java_cup-manual.noarch                                          1:0.11a-16.el7                                base   
javacc.noarch                                                   5.0-10.el7                                    base   
javacc-demo.noarch                                              5.0-10.el7                                    base   
javacc-javadoc.noarch                                           5.0-10.el7                                    base   
javacc-manual.noarch                                            5.0-10.el7                                    base   
javacc-maven-plugin.noarch                                      2.6-17.el7                                    base   
javacc-maven-plugin-javadoc.noarch                              2.6-17.el7                                    base   
javamail.noarch                                                 1.4.6-8.el7                                   base   
javamail-javadoc.noarch                                         1.4.6-8.el7                                   base   
javapackages-tools.noarch                                       3.4.1-11.el7                                  base   
javassist.noarch                                                3.16.1-10.el7                                 base   
javassist-javadoc.noarch                                        3.16.1-10.el7                                 base 

四、安装JDK

#安装java-1.8.0-openjdk开头的所有程序,-y表示自动回复yes。直到控制台返回Complete或者完毕,表示安装成功。
$ sudo yum install -y java-1.8.0-openjdk*

查看版本信息

$ sudo java -version
openjdk version "1.8.0_201"
OpenJDK Runtime Environment (build 1.8.0_201-b09)
OpenJDK 64-Bit Server VM (build 25.201-b09, mixed mode)
$ sudo javac -version
javac 1.8.0_201

五、查看JDK安装路径

已经通过yum安装好了jdk,那么安装到哪里去了呢?

查看方式一:

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

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

#:为Linux系统中环境变量分隔符
$ sudo echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

如何查看jdk的安装路径,JAVA_HOME呢?

$ sudo whereis javac
# 很明显,/usr/bin/javac默认加入了PATH路径(/usr/bin)中
javac: /usr/bin/javac /usr/share/man/man1/javac.1.gz
# 可以使用ll命令(或ls -l)查看它链接的地址
$ sudo ll /usr/bin/javac
# 可以看到/usr/bin/javac指向的地址是/etc/alternatives/javac
lrwxrwxrwx. 1 root root 22 3月  25 07:28 /usr/bin/javac -> /etc/alternatives/javac
# 继续查看是否有链接
$ sudo ll /etc/alternatives/javac
# 可以看到/etc/alternatives/javac 指向/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/bin/javac
lrwxrwxrwx. 1 root root 73 3月  25 07:28 /etc/alternatives/javac -> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/bin/javac
# 继续查看是否有链接
$ sudo ll /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/bin/javac
# 可以看到
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/bin/javac为一个真实的地址,而非一个软链接
-rwxr-xr-x. 1 root root 7376 3月 5 06:55 /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/bin/javac

所以JAVA_HOME就是/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64

查看方式二:

其实yum命令默认安装路径为/usr/lib/jvm,那么完整的安装路径是什么呢?

$ sudo ll /usr/lib/jvm
总用量 0
lrwxrwxrwx. 1 root root  26 3月  25 07:28 java -> /etc/alternatives/java_sdk
lrwxrwxrwx. 1 root root  32 3月  25 07:28 java-1.8.0 -> /etc/alternatives/java_sdk_1.8.0
lrwxrwxrwx. 1 root root  40 3月  25 07:28 java-1.8.0-openjdk -> /etc/alternatives/java_sdk_1.8.0_openjdk
drwxr-xr-x. 9 root root 109 3月  25 07:28 java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64   #其他都是软链接,只有这个和下面一个是真实地址。下面那个是debug,当然这个就是jdk的安装路径了,也就是JAVA_HOME地址。
drwxr-xr-x. 9 root root 109 3月  25 07:28 java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64-debug
lrwxrwxrwx. 1 root root  34 3月  25 07:28 java-openjdk -> /etc/alternatives/java_sdk_openjdk
lrwxrwxrwx. 1 root root  21 3月  25 07:28 jre -> /etc/alternatives/jre
lrwxrwxrwx. 1 root root  27 3月  25 07:28 jre-1.8.0 -> /etc/alternatives/jre_1.8.0
lrwxrwxrwx. 1 root root  35 3月  25 07:28 jre-1.8.0-openjdk -> /etc/alternatives/jre_1.8.0_openjdk
lrwxrwxrwx. 1 root root  51 3月  25 07:28 jre-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64 -> java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/jre
lrwxrwxrwx. 1 root root  57 3月  25 07:28 jre-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64-debug -> java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64-debug/jre
lrwxrwxrwx. 1 root root  29 3月  25 07:28 jre-openjdk -> /etc/alternatives/jre_openjdk

六、设置环境变量

#设置JAVA_HOME指向jdk的安装路径
$ sudo vi /etc/profile
#在文件最后加入如下内容:

# add java environment
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:${JRE_HOME}:${CLASSPATH}:$PATH

#修改/etc/profile之后让其生效
$ sudo source /etc/profile

#查看JAVA_HOME路径
$ sudo echo $JAVA_HOME
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64
#查看JRE_HOME路径
$ sudo echo $JRE_HOME
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/jre
#查看CLASSPATH路径
$ sudo echo $CLASSPATH
.:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/lib:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/jre/lib
#查看完整PATH路径
$ sudo echo $PATH
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/bin:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/jre:.:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/lib:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/jre/lib:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

 

posted @ 2019-03-25 08:24  wztone  阅读(1147)  评论(1编辑  收藏  举报