CentOS 卸载OpenJDK,安装OracleJDK

CentOS系统自带OpenJDK,但是我们平时开发都是用OracleJDK,下面记录如何卸载OpenJDK,再安装OracleJDK,供以后查阅。

卸载OpenJDK

查看CentOS是否安装JDK

使用java -version命令,如下所示:

1
2
3
4
$ java -version
openjdk version "1.8.0_91"
OpenJDK Runtime Environment (build 1.8.0_91-b14)
OpenJDK Server VM (build 25.91-b14, mixed mode)

查看已经安装的JDK信息

使用rpm -qa | grep java命令,如下所示:

1
2
3
4
5
6
$ rpm -qa | grep java 
java-1.8.0-openjdk-headless-1.8.0.91-0.b14.el6_7.i686
tzdata-java-2016d-1.el6.noarch
java-1.8.0-openjdk-1.8.0.91-0.b14.el6_7.i686

# 其他命令:rpm -qa | grep jdk

卸载OpenJDK

使用rpm -e --nodeps [java.xx],记得使用有权限用户,如下所示;

1
2
3
$ rpm -e --nodeps java-1.8.0-openjdk-headless-1.8.0.91-0.b14.el6_7.i686
$ rpm -e --nodeps tzdata-java-2016d-1.el6.noarch
$ rpm -e --nodeps java-1.8.0-openjdk-1.8.0.91-0.b14.el6_7.i686

检查是否卸载完成

使用java -version命令,如下所示;

1
2
$ java -version
bash: java: command not found

出现以上信息,表示卸载成功。

安装Oracle JDK

下载Oracle JDK

使用CentOSwget命令下载,虽然Oracle官方不允许直接下载JDK,但是我们可以获取下载地址后拷贝出来下载,授权后的下载地址wget http://download.oracle.com/otn/java/jdk/7u80-b15/jdk-7u80-linux-i586.rpm?AuthParam=1466749556_f8c61ecf9ec04d793d7f28fc427ea6d7,下载结果如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
$ wget http://download.oracle.com/otn/java/jdk/7u80-b15/jdk-7u80-linux-i586.rpm?AuthParam=1466749556_f8c61ecf9ec04d793d7f28fc427ea6d7

--2016-06-24 14:24:26-- http://download.oracle.com/otn/java/jdk/7u80-b15/jdk-7u80-linux-i586.rpm?AuthParam=1466749556_f8c61ecf9ec04d793d7f28fc427ea6d7
Resolving download.oracle.com... 184.25.56.53, 184.25.56.42
Connecting to download.oracle.com|184.25.56.53|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 136775360 (130M) [application/x-redhat-package-manager]
Saving to: `jdk-7u80-linux-i586.rpm?AuthParam=1466749556_f8c61ecf9ec04d793d7f28fc427ea6d7'

100%[=============================================================================================>] 136,775,360 16.1M/s in 7.6s

2016-06-24 14:24:34 (17.2 MB/s) - `jdk-7u80-linux-i586.rpm?AuthParam=1466749556_f8c61ecf9ec04d793d7f28fc427ea6d7' saved [136775360/136775360]

重命名JDK

由于下载后的JDK名字为jdk-7u80-linux-i586.rpm?AuthParam=1466749556_f8c61ecf9ec04d793d7f28fc427ea6d7,所以我们需要重命名一下,命令如下:

1
2
3
4
5
-- 重命名
$ mv jdk-7u80-linux-i586.rpm\?AuthParam\=1466749556_f8c61ecf9ec04d793d7f28fc427ea6d7 jdk-7u80-linux-i586.rpm

-- 修改文件权限
$ chmod 777 jdk-7u80-linux-i586.rpm

安装JDK

执行如下命令,选择yes直到安装完毕。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ rpm -ivh jdk-7u80-linux-i586.rpm 

Preparing... ########################################### [100%]
1:jdk ########################################### [100%]
Unpacking JAR files...
rt.jar...
jsse.jar...
charsets.jar...
tools.jar...
localedata.jar...
jfxrt.jar...
plugin.jar...
javaws.jar...
deploy.jar...

检查是否安装成功

使用java -version命令,如下所示:

1
2
3
4
$ java -version
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) Client VM (build 24.80-b11, mixed mode, sharing)