检查Linux系统是否已安装jdk
[root@CentOS6]# rpm -qa | grep java
tzdata-java-2016c-1.el6.noarch
java-1.6.0-openjdk-1.6.0.38-1.13.10.4.el6.x86_64
java-1.7.0-openjdk-1.7.0.99-2.6.5.1.el6.x86_64
卸载系统自带的java
[root@CentOS6]# rpm -qa | grep java | xargs rpm -e --nodeps
查看卸载结果
[root@CentOS6]# java -version
bash: /usr/bin/java: No such file or directory
卸载成功
下载jdk
http://www.Oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
jdk-7u79-linux-x64.rpm
jdk-7u79-linux-x64.tar.gz
第一种方法:rpm包安装
rpm包导入用户root目录下
[root@CentOS6]# ls
anaconda-ks.cfg id_dsa.pub install.log install.log.syslog
jdk-7u79-linux-x64.rpm
赋予权限
[root@CentOS6]# chmod 755 jdk-7u79-linux-x64.rpm
安装
[root@CentOS6]# rpm -ivh jdk-7u79-linux-x64.rpm
Preparing... ########################################### [100%]
1:jdk ########################################### [100%]
Unpacking JAR files...
rt.jar...
jsse.jar...
charsets.jar...
tools.jar...
localedata.jar...
jfxrt.jar...
安装完成
第二种方式jdk的二进制gz包安装
将jdk的二进制gz包上传至服务器的root目录下
root@linux3 ~]# mkdir /usr/java
将二进制包放在 /usr/java/ 下并解压
[root@CentOS6 ~]# mv jdk-7u71-linux-x64.tar.gz /usr/java
[root@CentOS6 ~]# cd /usr/java/
[root@CentOS6 java]# ls
jdk-7u71-linux-x64.tar.gz
[root@CentOS6 java]# tar xvf jdk-7u71-linux-x64.tar.gz
等待解压完成
[root@CentOS6 java]# ls
jdk1.7.0_71 jdk-7u71-linux-x64.tar.gz
删除压缩包
[root@CentOS6 java]# rm -rf jdk-7u71-linux-x64.tar.gz
[root@CentOS6 java]# ls
jdk1.7.0_71
[root@CentOS6 java]# vi /etc/profile
添加
export JAVA_HOME=/usr/java/jdk1.7.0_71
export JRE_HOME=$JAVA_HOME/jre
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=./:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
保存
使配置立即生效
[root@CentOS6 java]# source /etc/profile
配置用户java环境变量 立即生效
添加
export JAVA_HOME=/usr/java/jdk1.7.0_71
export JRE_HOME=$JAVA_HOME/jre
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=./:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
[root@CentOS6 java]# vi ~/.bashrc
[root@CentOS6 java]# source ~/.bashrc
最后检查环境变量
[root@CentOS6 java]# echo $JAVA_HOME
/usr/java/jdk1.7.0_71
[root@CentOS6 java]# echo $CLASSPATH
./:/usr/java/jdk1.7.0_71/lib:/usr/java/jdk1.7.0_71/jre/lib
[root@CentOS6 java]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/java/jdk1.7.0_55/bin:/usr/java/jdk1.7.0_55/bin:/root/bin:/usr/java/jdk1.7.0_71/bin:/usr/java/jdk1.7.0_71/bin
安装成功
[root@CentOS6 java]# java -version
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)