#!/bin/bash
# update-java script
# Supported versions from the script. You may add more version numbers according to your needs.
supported_versions=( 8 11 )
# Validate that an argument has been given
if [ -z "$1" ]; then
echo -e "No argument given. You should set as first argument the preferred java version.\nAvailable values: ${supported_versions[@]}"
exit
fi
# Java "/bin" of each version. Be careful, each path should have the same index with the supported_versions array.
version_path=( "/usr/lib/jvm/jdk1.8.0_171/bin" "/usr/lib/jvm/java-11-openjdk-amd64/bin")
# Configure the alternatives
found=0
for i in "${!supported_versions[@]}"; do
if [[ ${supported_versions[$i]} -eq $1 ]]; then
update-alternatives --set java ${version_path[$i]}/java
update-alternatives --set javac ${version_path[$i]}/javac
found=1
fi
done
# If a wrong version number has been given
if [ $found -ne 1 ]; then
echo "You didn't provide a version of: ${supported_versions[@]}"
fi
如果将脚本保存为“update-java”,然后设置可执行权限,您将能够像下面这样运行。
$sudo update-java 12
You didn't provide a version of: 8 11
$sudo update-java
No argument given. You should set as first argument the preferred java version.
Available values: 8 11
$sudo update-java 11
你的版本有
--set
吗?您可以使用
alternatives --auto <name>
自动选择最高优先级的选项。一个例子:
将选择更高优先级的版本 (20)
/usr/java/latest/bin/javac
使用
grep
:或者
您可以使用下面的脚本。
您应该根据您的系统配置java“/bin”目录的路径和每个版本的编号。
如果将脚本保存为“update-java”,然后设置可执行权限,您将能够像下面这样运行。