###下载jdk安装包
wget http://download.oracle.com/otn-pub/java/jdk/8u121-b13/e9e7ea248e2c4826b92b3f075a80e441/jdk-8u121-linux-x64.tar.gz
###解压安装包
tar -zxvf jdk-8u11-linux-x64.tar.gz
###创建java目录
mkdir -p /usr/java/
mv jdk1.8.0_11/ /usr/java/
###创建软链接
ln -s /usr/java/jdk1.8.0_11/ default 创建默认版本的jdk软链接
ln -s /usr/java/default latest 将默认的创建为最新版本的软链接
###添加jdk的环境变量
#vim /etc/profile
export JAVA_HOME=/usr/java/default
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/lib:$JAVA_HOME/jre/bin:$PATH:$HOME/bin:$PATH
export JAVA_TOOLS=$JAVA_HOME/lib/tools.jar
export PS1="\[\e]0;\a\]\n\[\e[1;32m\]\[\e[1;33m\]\H\[\e[1;35m\]<\$(date +\"%Y-%m-%d %T\")> \[\e[32m\]\w\[\e[0m\]\n\u>\\$ " 添加环境变量高亮显示
###检查jdk环境是否OK
source /etc/profile
su -l
root># java -version
java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)
###JDK升级脚本 测试环境jdk1.8.0_11升级,新的jdk安装包放在192.168.91.33:/mfs/ShareFile/software目录中!
要是需要回滚至之前的jdk1.7.0_15的版本,只需要在/usr/java/目录中更改latest软连接的指向即可!
#!/bin/bash
#This is an upgrade java script
#Written by manbo.xu on 23th,july,2014
SOURCEDIR=/mfs/ShareFile/software
DESTINATIONDIR=/usr/java
echo "Upgrade the java to jdk1.8.0_11"
cd $SOURCEDIR
[ -e jdk-8u11-linux-x64.tar.gz ] || wget http://download.oracle.com/otn-pub/java/jdk/8u11-b12/jdk-8u11-linux-x64.tar.gz
if [ $? -ne 0 ];then
echo "-------------ERROR,Skip the Windows-----------------"
exit 1
else
sleep 3
cd $SOURCEDIR
tar -zvxf $SOURCEDIR/jdk-8u11-linux-x64.tar.gz -C $DESTINATIONDIR && chown -R root.root $DESTINATIONDIR/jdk1.8.0_11
cd $DESTINATIONDIR
#if [ -f /usr/java/default ];then
if [ -L /usr/java/default ];then #-----因为default是链接文件,所以需要用-L或者-h参数来判断
rm -rf /usr/java/default
ln -s $DESTINATIONDIR/latest default
echo "-----------------default is deleting----------------"
else
ln -s $DESTINATIONDIR/latest default
echo "-----------------default is create------------------"
fi
cd $DESTINATIONDIR
#if [ -f /usr/java/latest ]
if [ -h /usr/java/latest ] #-----因为latest是链接文件,所以需要用-L或者-h参数来判断
then
rm -rf latest
ln -s $DESTINATIONDIR/jdk1.8.0_11 latest
echo "----------------Successfully Upgrade java!-----------"
else
ln -s $DESTINATIONDIR/jdk1.8.0_11 latest
echo "----------------latest is create and Upgrade java Successfully------------"
fi
export JAVA_HOME=/usr/java/default
export CLASSPATH=.:$JAVA_HOME/lib/:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/bin:$PATH:
export JAVA_TOOLS=$JAVA_HOME/lib/tools.jar
#source /etc/profile
fi
###测试ok