CentOS7卸载自带的openJDK,并安装oracle-jdk1.8 – 记忆角落

CentOS7卸载自带的openJDK,并安装oracle-jdk1.8

/ 0评 / 0

1.删除旧版(自带jdk):

rpm -qa|grep jdk
rpm -e java-xxx –nodeps

2.安装jdk:

rpm -ivh jdk-8u112-linux-x64.rpm

3.确保使用的java版本是上面安装的版本。

java -version

有时候可能运到安装JDK后,系统还是使用自带的open jdk。

在这种情况一般需要把/usr/bin/下java相关的软链接重新建立。例子:

rm /usr/bin/java
ln -s /usr/java/default/bin/java /usr/bin/java
rm /usr/bin/javac
ln -s /usr/java/default/bin/javac /usr/bin/javac
rm /usr/bin/javadoc
ln -s /usr/java/default/bin/javadoc /usr/bin/javadoc
rm /usr/bin/javafxpackager
ln -s /usr/java/default/bin/javafxpackager /usr/bin/javafxpackager
rm /usr/bin/javah
ln -s /usr/java/default/bin/javah /usr/bin/javah
rm /usr/bin/javap
ln -s /usr/java/default/bin/javap /usr/bin/javap
rm /usr/bin/javapackager
ln -s /usr/java/default/bin/javapackager /usr/bin/javapackager
rm /usr/bin/java-rmi.cgi
ln -s /usr/java/default/bin/java-rmi.cgi /usr/bin/java-rmi.cgi
rm /usr/bin/javaws
ln -s /usr/java/default/bin/javaws /usr/bin/javaws

下面提供一个一键脚本吧,代码如下:

#!/bin/bash
declare INSTALLVERSION
declare INSTALLFILE

isNeedInstall()
{
    local versioninfo=`java -version 2>&1`
    if [ $? -eq 0 ]
    then 
       local version=`java -version 2>&1 | awk '{if(NR==1){gsub(/"/,""); print $3}}'`
       if [[ $versioninfo == "openjdk"* || $versioninfo == *"IBM"* ]]
       then 
           echo "install openjdk or ibm jdk"
           return 1
        elif [[ "${INSTALLVERSION}" == "${version}" ]]
        then
           echo "The oracle jdk(${INSTALLVERSION}) has installed."
           return 0
        else
           echo "This machine installed oracle jdk, but the version is wrong."
           return 1
        fi
    else
        echo "This machine doesn't install oracle jdk."
        return 1
    fi
}

find_installfile()
{
   local filename=`ls |grep ^jdk.*\.rpm`
   if [[ -f ${filename} ]]
   then
       INSTALLFILE=${filename}
       return 0
   else
       echo "can't find jdk install file."
       exit 1
   fi   
}

create_javalink()
{
    local java_home=$1
    local links=(java javac javadoc javafxpackager javah javap javapackager java-rmi.cgi javaws)
    for linkname in ${links[@]}
    do
       if [[ -f "/usr/bin/${linkname}" ]]
       then
          rm -f /usr/bin/${linkname}
       fi
         ln -s ${java_home}/bin/java /usr/bin/${linkname}
    done
}

install()
{
    if [ ! -f ${INSTALLFILE} ]
    then
       echo "the file ${INSTALLFILE} doesn't exits. "
       return 1
    else
       rpm -ivh ${INSTALLFILE}
       if [[ ! $? -eq 0 ]]
       then
          echo "install ${INSTALLFILE} error."
          return 1
       else
           local version=`java -version 2>&1 | awk '{if(NR==1){gsub(/"/,""); print $3}}'`
           if [[ ! "${version}" == "${INSTALLVERSION}" ]]
           then
              echo "create sybomic link."
              if [ -d "/usr/java" ]
              then
                 local java_home=`ls /usr/java | grep ${INSTALLVERSION}`
                 if [[ "$java_home" == "" ]]
                 then
                     echo "can't find the java install directory."
                     return 1
                 else
                     create_javalink "/usr/java/${java_home}"
                     version=`java -version 2>&1 | awk '{if(NR==1){gsub(/"/,""); print $3}}'`
                     if [[ "${version}" == "${INSTALLVERSION}" ]]
                     then
                        echo "install jdk successfully."
                        return 0
                     else
                        echo "unkown error. please check in manul."
                        return 1
                     fi
                 fi
              else
                 echo "/usr/java doesn't exist."
                 return 1
              fi  
           else
               echo "install jdk successfully."
               return 0
           fi
        fi
    fi    
}

if [ $# -eq 1 ]
then
   INSTALLVERSION=$1
   find_installfile
   isNeedInstall
   if [ $? -eq 1 ]
   then
      install
      if [ $? -eq 0 ]
      then
         exit 0
      else
         exit 1
      fi
   else
      exit 0
   fi 
else
   echo "params error.Usage: sh jdk_instanller.sh 1.8.0_181"
   exit 1
fi

运行脚本时,需要添加参数,参数名为你安装java版本号,如:

bash jdk_instanller.sh 1.8.0_181

1.8.0_181 就是你的jdk版本号


发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注