Linux版本:CentOS_7.3_x64
jdk版本:jdk-8u144-linux-x64
系统操作用户:root
一、查看并卸载系统中的 jdk
(1)查看系统中 jdk 相关软件:
[root@RemoteEshaon ~]# rpm -qa | grep java
输出:
javapackages-tools-3.4.1-6.el7_0.noarch
python-javapackages-3.4.1-6.el7_0.noarch
java-1.7.0-openjdk-1.7.0.75-2.5.4.2.el7_0.x86_64
tzdata-java-2015a-1.el7.noarch
java-1.7.0-openjdk-headless-1.7.0.75-2.5.4.2.el7_0.x86_64
(2)卸载 jdk(若存在)
[root@RemoteEshaon ~]# rpm -e --nodeps javapackages-tools-3.4.1-6.el7_0.noarch
[root@RemoteEshaon ~]# rpm -e --nodeps python-javapackages-3.4.1-6.el7_0.noarch
[root@RemoteEshaon ~]# rpm -e --nodeps tzdata-java-2015a-1.el7.noarch
[root@RemoteEshaon ~]# rpm -e --nodeps java-1.7.0-openjdk-headless-1.7.0.75-2.5.4.2.el7_0.x86_64
(3)查看卸载情况:
[root@RemoteEshaon ~]# rpm -qa | grep java
[root@RemoteEshaon ~]#
二、安装 jdk
Linux系统中安装jdk常用方法有三种,这里采用 rpm 安装 jdk。
根据Linux 目录结构的特点,准备将jdk安装到 /usr 目录下。
(1)在目录/usr
下新建java
目录:
[root@RemoteEshaon ~]# mkdir /usr/java
[root@RemoteEshaon ~]# cd /usr/java
(2)到官网下载 jdk-8u144-linux-x64.rpm
使用 wget 命令【参考stackoverflow】
[root@RemoteEshaon ~]# cd /usr/java
[root@RemoteEshaon java]# wget --no-cookies --no-check-certificate --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u144-b01/090f390dda5b47b9b721c7dfaa008135/jdk-8u144-linux-x64.rpm
(3)安装 jdk,默认安装到当前路径 /usr/java
[root@RemoteEshaon java]# sudo yum localinstall jdk-8u144-linux-x64.rpm
//...
//...
//...
Installed:
jdk-8u144-linux-x64.rpm 2000:1.8.0_102-fcs
Complete!
[root@RemoteEshaon java]#
(4)查看 jdk 安装目录
[root@RemoteEshaon java]# cd /usr/java
[root@RemoteEshaon java]# ls -lsah
total 12K
4.0K drwxr-xr-x 3 root root 4.0K Aug 31 15:32 .
4.0K drwxr-xr-x. 14 root root 4.0K Jul 22 13:15 ..
0 lrwxrwxrwx 1 root root 16 Aug 31 15:24 default -> /usr/java/latest
4.0K drwxr-xr-x 9 root root 4.0K Aug 31 15:24 jdk1.8.0_144
0 lrwxrwxrwx 1 root root 22 Aug 31 15:24 latest -> /usr/java/jdk1.8.0_144
[root@RemoteEshaon java]#
(5)在当前目录下 /usr/java
检验安装是否成功
[root@RemoteEshaon java]# java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
[root@RemoteEshaon java]#
至此,安装成功!
(5)删除 RPM 文件
[root@RemoteEshaon java]# rm -rf jdk-8u144-linux-x64.rpm
三、在 root 用户下配置 JDK 环境变量
编辑环境变量配置文件:用Vim打开配置文件/etc/profile
,在文件末尾加入下面代码,保存并退出。
(1)切换目录,打开配置文件/etc/profile
[root@RemoteEshaon java]# cd /etc/
[root@RemoteEshaon etc]# vim profile
(2)在文件末尾加入下面代码:
82 # User specific environment and startup programs
83 export JAVA_HOME=/usr/java/jdk1.8.0_144
84 export JRE_HOME=/usr/java/jdk1.8.0_144/jre
85 export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
86 export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
(3)使环境变量即时生效:
[root@RemoteEshaon etc]# source /etc/profile
(4)测试 jdk 环境是否配置成功
[root@RemoteEshaon etc]# cd
[root@RemoteEshaon ~]# java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
[root@RemoteEshaon ~]#
至此,环境变量配置成功!
【参考】
[1] CentOS 7安装JDK
[2] How to install Oracle JDK 8 on CentOS
[3] LInux安装jdk的三种方法
[4] CentOS 7 配置Java环境变量
[5] 每天一个linux命令(23):Linux 目录结构