#!/bin/bash
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>开始安装jdk8环境.."
CRTDIR=../docker-images
jdkTargz="${CRTDIR}/jdk-8u311-linux-x64.tar.gz"
echo "${jdkTargz}"
# 检查原先是否已配置java环境变量
checkExist() {
jdk1=$(grep -n "export JAVA_HOME=.*" /etc/profile | cut -f1 -d':')
if [ -n "$jdk1" ]; then
echo "JAVA_HOME已配置,删除内容"
sed -i "${jdk1}d" /etc/profile
fi
jdk2=$(grep -n "export CLASSPATH=.*\$JAVA_HOME.*" /etc/profile | cut -f1 -d':')
if [ -n "$jdk2" ]; then
echo "CLASSPATH路径已配置,删除内容"
sed -i "${jdk2}d" /etc/profile
fi
jdk3=$(grep -n "export PATH=.*\$JAVA_HOME.*" /etc/profile | cut -f1 -d':')
if [ -n "$jdk3" ]; then
echo "PATH-JAVA路径已配置,删除内容"
sed -i "${jdk3}d" /etc/profile
fi
}
# 查询是否有jdk.tar.gz
if [ -e $jdkTargz ]; then
echo "— — 存在jdk压缩包 — —"
echo "正在解压jdk压缩包..."
tar -zxvf ${jdkTargz} -C /opt
if [ -e "/opt/install/java" ]; then
echo "存在该文件夹,删除..."
rm -rf /opt/install/java
fi
echo "---------------------------------"
echo "正在建立jdk文件路径..."
echo "---------------------------------"
mkdir -p /opt/install/java/
mv /opt/jdk1.8.0_311 /opt/install/java/java8
# 检查配置信息
checkExist
echo "---------------------------------"
echo "正在配置jdk环境..."
sed -i '$a export JAVA_HOME=/opt/install/java/java8' /etc/profile
sed -i '$a export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar' /etc/profile
sed -i '$a export PATH=$PATH:$JAVA_HOME/bin' /etc/profile
echo "---------------------------------"
echo "JAVA环境配置已完成..."
echo "---------------------------------"
echo "正在重新加载配置文件..."
echo "---------------------------------"
source /etc/profile
echo "配置版本信息如下:"
java -version
java -version >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "JDK已成功安装!"
else
echo "JDK未正确安装,请检查安装步骤和要求。"
fi
else
echo "未检测到安装包,请将安装包放到/opt目录下"
fi
07-17
10-14
03-18
01-17
07-22
524
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)