ubuntu/centos下安装jdk shell脚本,用root执行。支持32位系统和64位系统,github链接
#!/bin/bash
# jdk install
# 请将下载的jdk-xxx-linux-xxx.tar.gz包与此脚本放置到同一目录
# 授予此脚本可执行权限(chmod +x install_jdk.sh)
# 在终端执行此脚本开始安装(./文件名)
# 注意:不可有多个版本的jdk包!
# 为了使配置的环境变量生效,安装完成后你应该重新登陆。
echo "Please select you want to install the JDK version?"
select jdk_version in "JDK1.7" "JDK1.8" ; do
break;
done
jvmpath=/usr/lib/jvm/
# 不存在
if [ ! -d "$jvmpath" ]; then
echo "正在创建$jvmpath目录"
sudo mkdir $jvmpath
echo "目录$jvmpath创建成功"
fi
jdkfile=$(ls | grep jdk-*-linux-*.tar.gz)
jdkdirname=""
if [ "JDK1.7"="$jdk_version" ]; then
jdkdirname="jdk1.7.0_75"
else
jdkdirname="jdk1.8.0_20"
fi
os_version=`uname -a`
echo $os_version
architecture="64"
echo "$os_version" | grep -q "$architecture"
if [ $? -eq 0 ]
then
echo "您正在使用64位操作系统,为您选择64位JDK"
# 不存在即去外网下载jdk文件
if [ ! -f "$jdkfile" ]; then
if [ "JDK1.7"="$jdk_version" ]; then
wget http://7vil1r.com1.z0.glb.clouddn.com/jdk-7u75-linux-x64.tar.gz
else
wget http://7vil1r.com1.z0.glb.clouddn.com/jdk-8u20-linux-x64.gz
fi
fi
#sudo chown -R jiangxin:jiangxin /usr/lib/jvm/jdk1.7.0_75
else
echo "您正在使用32位操作系统,为您选择32位JDK和eclipse"
# 不存在即去外网下载jdk文件
if [ ! -f "$jdkfile" ]; then
if [ "JDK1.7"="$jdk_version" ]; then
wget http://7vil1r.com1.z0.glb.clouddn.com/jdk-7u75-linux-i586.gz
else
wget http://7vil1r.com1.z0.glb.clouddn.com/jdk-8u20-linux-i586.gz
fi
fi
fi
jdkfile=$(ls | grep jdk-*-linux-*.tar.gz)
if [ -f "$jdkfile" ]; then
sudo tar -zxvf $jdkfile -C /usr/lib/jvm/
echo "安装JDK成功"
echo "配置环境变量"
# touch environment
# echo "PATH=\"$PATH:/usr/lib/jvm/$jdkdirname/bin\"" >> environment
# echo "JAVA_HOME=/usr/lib/jvm/$jdkdirname" >> environment
# echo "CLASSPATH=.:%JAVA_HOME%/lib/dt.jar:%JAVA_HOME%/lib/tools.jar" >> environment
# sudo mv /etc/environment /etc/environment.backup.java
# sudo mv environment /etc
# source /etc/environment
mv ~/.bashrc ~/.bashrc.backup.java
cat ~/.bashrc.backup.java >> ~/.bashrc
echo "PATH=\"$PATH:/usr/lib/jvm/$jdkdirname/bin\"" >> ~/.bashrc
echo "JAVA_HOME=/usr/lib/jvm/$jdkdirname" >> ~/.bashrc
echo "CLASSPATH=.:%JAVA_HOME%/lib/dt.jar:%JAVA_HOME%/lib/tools.jar" >> ~/.bashrc
source ~/.bashrc
echo "配置环境成功"
# 如果有多个java版本需要进行以下配置(包括openjdk)
echo "设置默认jdk"
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/$jdkdirname/bin/java 300
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/$jdkdirname/bin/javac 300
sudo update-alternatives --config java
# echo "设置默认jdk成功"
echo "测试是否安装成功"
java -version
echo "安装成功"
fi
卸载脚本
#!/bin/bash
echo "正在删除相关文件"
sudo rm -rf /usr/lib/jvm/
wait
echo "删除相关文件成功"
echo "恢复配置文件"
# sudo rm -f /etc/environment
# sudo mv /etc/environment.backup.java /etc/environment
sudo rm /usr/bin/java /usr/bin/javac
sudo rm /etc/alternatives/java /etc/alternatives/javac
mv ~/.bashrc.backup.java ~/.bashrc
echo "恢复配置文件成功"