1、在指定目录下创建一个自动运行的脚本文件:
vim install_jdk8.sh
2、编写脚本:
#!/bin/bash
echo "java8 install begin..."
#这里是放下载文件的目录,可任意改
cd /usr/local/my/soft
#只是一个临时变量而已,也是作为JDK的主目录,jdk就安装在这里
java_home=/usr/local/java8
# 下载 jdk(下载地址可自己定)
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u141-b15/336fa29ff2bb4ef291e347e091f7f4a7/jdk-8u141-linux-x64.tar.gz"
# 创建目录且解压到指定目录
mkdir -p ${java_home}
tar -zxvf jdk-8u141-linux-x64.tar.gz -C ${java_home} --strip-components 1
# 配置环境变量 PATH
echo "# java8 environment----$(date)----" >> /etc/profile
echo "export JAVA_HOME=${java_home}" >> /etc/profile
echo "export CLASSPATH=.:\$JAVA_HOME/jre/lib/rt.jar:\$JAVA_HOME/lib/dt.jar:\$JAVA_HOME/lib/tools.jar" >> /etc/profile
echo "export PATH=\$PATH:\$JAVA_HOME/bin" >> /etc/profile
echo "# java8 environment-----end-----" >> /etc/profile
source /etc/profile
echo "java8 install success!"
# 修改JDK与JRE
alternatives --install /usr/bin/java java ${java_home}/bin/java 80
alternatives --install /usr/bin/javac javac ${java_home}/bin/javac 80
alternatives --install /usr/bin/jar jar ${java_home}/bin/jar 80
echo "-------Update java, Please Select [/usr/local/java8/bin/java]------------- "
alternatives --config java
echo "------Update javac, Please Select [ /usr/local/java8/bin/javac]-------------"
alternatives --config javac
3、运行脚本
. install_jdk8.sh
根据提示选择,我这里是选择:4