使用方法

source install_jdk8.sh

install_jdk8.sh接收$1参数,可指定安装路径,默认安装路径为/usr/local

install_jdk8.sh

#!/bin/bash

current_dir=`pwd`
directory=$1

function select_installation_directory(){
  if [[ -z ${directory} ]];
  then
    directory='/usr/local'
  fi
}

function delete_default_jdk(){
  for i in $(rpm -qa | grep jdk | grep -v grep)
  do
    echo "卸载jdk -> "$i
    rpm -e --nodeps $i
  done
}

function install_jdk(){
  if [[ -n $(rpm -qa | grep jdk | grep -v grep) ]];
  then
    echo "-->卸载默认jdk失败"
  else
    tar -zxvf "${current_dir}"/jdk-8u311-linux-x64.tar.gz -C ${directory}
    #配置环境变量
    if ! grep "JAVA_HOME=${directory}/jdk1.8.0_311" /etc/profile
    then
        echo "export JAVA_HOME=${directory}/jdk1.8.0_311" >>/etc/profile
        echo -e 'export CLASSPATH=.:${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar'>>/etc/profile
        echo -e 'export PATH=$PATH:${JAVA_HOME}/bin'>>/etc/profile
        source /etc/profile
    fi
  fi
  echo "java version:"
  java -version
}

function Main(){
  select_installation_directory;
  delete_default_jdk;
  install_jdk;
}
Main;

jdk下载地址

https://www.sky12580.cn/upload/2022/04/jdk-8u311-linux-x64.tar.gz