环境清理

卸载linux安装的jdk

rpm -qa | grep java
java-1.6.0-openjdk-1.6.0.0-11.1.13.4.el6.x86_64
java-1.7.0-openjdk-1.7.0.65-2.5.1.2.el6_5.x86_64
rpm -e java-1.7.0-openjdk-1.7.0.65-2.5.1.2.el6_5.x86_64

安装(JDK6)

将下载好的jdk-6u45-linux-x64.bin复制到/usr/local

cp jdk-6u45-linux-x64.bin /usr/local
cd /usr/local/
chmod +x  jdk-6u45-linux-x64.bin
./jdk-6u45-linux-x64.bin
ln -s /usr/local/jdk1.6.0_45 /usr/local/java
ln -s /usr/local/java/bin/* /usr/bin/
rm jdk-6u45-linux-x64.bin

安装(JDK7)

wget http://download.oracle.com/otn-pub/java/jdk/7u80-b15/jdk-7u80-linux-x64.tar.gz
tar -zxvf jdk-7u80-linux-x64.tar.gz
mv jdk1.7.0_80 /usr/local/jdk1.7
ln -s /usr/local/jdk1.7/bin/* /usr/sbin/
echo "export JAVA_HOME=/usr/local/jdk1.7" >> /etc/profile
echo "export CLASSPATH=$CLASSPATH:.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:/usr/local/resin/lib/resin.jar" >> /etc/profile
echo "export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin" >> /etc/profile
source /etc/profile

批量删除/usr/sbin下的jdk1.7的软link

find /usr/sbin/ -type l | xargs ls -l | grep jdk1.7 | awk '{print $9}' | xargs rm -f

配置环境变量

vi /etc/profile
echo "export JAVA_HOME=/usr/local/java" >> /etc/profile
echo "export CLASSPATH=$CLASSPATH:.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:/usr/local/resin/lib/resin.jar" >> /etc/profile
echo "export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin" >> /etc/profile

让环境变量立即生效

source /etc/profile

检查环境变量是否生效

echo $JAVA_HOME;echo $CLASSPATH;echo $PATH

验证

在登录后输入java -version

看到如下提示即表示成功

java version "1.6.0_45"
Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)

参考链接