Linux Java JDK多版本管理自定义工具
为什么有其他工具还要自定义?
之前用过update-alternatives 这个命令管理 但是如果JDK版本更新了,文件夹一变,你就要重新改注册,你要各种拷贝目录…
我的工具基本原理
通过软连接的方式在环境变量中配置一个固定的路径,这样每次要切换JDK版本时,只要改变下这个软连接路径即可,其他不要动。
即使增加新版本,只要将JDK放到/usr/lib/jvm/目录下即可通过运行工具选择切换。
运行工具前的步骤
按照正常情况,JDK默认安装的位置是/usr/lib/jvm/,所以我也就按照这个默认的位置来,把JDK的文件夹(如:jdk1.8.0_231)不管是安装还是拷贝的方式都放到这个目录下。
然后,如果是第一次使用,还是要正常配置环境变量的,不过要按照如下规则配置,其中这个路径不能变(/usr/lib/jvm/java_home)
#set oracle jdk environment
export JAVA_HOME=/usr/lib/jvm/java_home ## Don't change this directory!!!
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
最后运行下命令source /etc/profile
激活你设置的环境变量。
接着就可以运行工具了。
上代码
将下面代码保存为sh文件即可,然后直接运行。没有位置要求,如果经常用可以放到bin目录下使用。
#!/bin/bash
echo
echo "==================================================================="
echo "Please follow 3 steps as below before you select the JDK version!"
echo "Step 1:"
echo
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "Please put jdk folder in '/usr/lib/jvm/', this program will scan
JDK in this directory!"
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo
echo "Step 2:"
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "Please append the text as below to file '/etc/profile'!"
echo "#----------------------------------------------------------"
echo "#set oracle jdk environment
export JAVA_HOME=/usr/lib/jvm/java_home ## Don't change this directory!!!
export JRE_HOME=\${JAVA_HOME}/jre
export CLASSPATH=.:\${JAVA_HOME}/lib:\${JRE_HOME}/lib
export PATH=\${JAVA_HOME}/bin:\$PATH "
echo "#----------------------------------------------------------"
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo
echo "Step 3:"
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "!!!Run command 'source /etc/profile' to activate the configuration!"
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo
echo
echo -n "Do you already has followed the three steps before? {[yes]|no}:"
read answer
if [[ $answer =~ ^[N|n]o$ ]]; then
echo "exit."
exit
fi
cd /usr/lib/jvm/
echo "There are JDK versions as below:"
jdks=`eval ls -d */|grep jdk`
#echo $jdks
jdkArr=($jdks);
#echo ${jdkArr[1]} --- ${!jdkArr[@]}
# print JDK directories and number
echo "============================="
printf "%s\t%s\n" "NO." "JDK"
for i in "${!jdkArr[@]}";
do
printf "%s\t%s\n" "$i" "${jdkArr[$i]}"
done
echo "============================="
# wait input and read number
echo -n "please enter an number before the JDK:"
read num
if [[ ! $num =~ ^-?[0-9]+$ ]]; then #validate is number or not
echo "$num is not number. exit."
exit
fi
if echo "${!jdkArr[@]}" | grep -w $num &>/dev/null; then # validate number is in scope or not
echo "The JDK you select is "${jdkArr[$num]}
else
echo "The number you input is invalid. exit."
exit
fi
# create soft link
sudo ln -s $PWD/${jdkArr[$num]} $PWD/java_home
echo "Done and enjoy!"
echo "============================="
java -version
echo
echo
exit
运行效果
谢谢大家!