Skip to content

Instantly share code, notes, and snippets.

@efann
Last active November 7, 2022 20:55
Show Gist options
  • Save efann/b7f281b359a811299d46b20814667867 to your computer and use it in GitHub Desktop.
Save efann/b7f281b359a811299d46b20814667867 to your computer and use it in GitHub Desktop.
change-java-version.sh
#!/bin/bash
# License: Eclipse Public License - v 2.0 (https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html)
# Updated on November 7, 2022
if [[ "$EUID" -ne 0 ]]
then
echo -e "\nThis script must be run as root. Exiting. . . .\n\n"
exit 1
fi
if [ -z "$1" ]
then
echo -e "\nOne must pass the Java version to enable. Exiting. . . .\n\n"
exit 1
fi
# Check for integer parameter
if ! [[ "$1" =~ ^[0-9]+$ ]]
then
echo -e "\nOne must pass a valid number like 8 or 11 Exiting. . . .\n\n"
exit 1
fi
lcVersion="$1"
echo -e "=> --------------------------------------"
echo -e "=> apt remove --purge openjdk* java-common default-jdk -y"
echo -e "=> --------------------------------------\n"
sudo apt remove --purge openjdk* java-common default-jdk -y
echo -e "=> --------------------------------------"
echo -e "=> apt autoremove --purge -y"
echo -e "=> --------------------------------------\n"
sudo apt autoremove --purge -y
echo -e "=> --------------------------------------"
echo -e "=> update-alternatives --config java"
echo -e "=> --------------------------------------\n"
sudo update-alternatives --config java
echo -e "=> --------------------------------------"
echo -e "=> apt install openjdk-${lcVersion}-jdk -i"
echo -e "=> --------------------------------------\n"
sudo apt install "openjdk-${lcVersion}-jdk" -y
echo -e "=> --------------------------------------"
echo "=> Java Version Info"
echo -e "=> --------------------------------------\n"
java -version
if [ -z "${JAVA_HOME:-}" ]
then
echo -e "\nJAVA_HOME has not been set, and that's probably okay.\n\n"
else
lcJavaHome="${JAVA_HOME}"
if [ ! -d "${lcJavaHome}" ]
then
echo -e "\nThe folder referenced by JAVA_HOME, ${lcJavaHome}, does not exist.\n"
else
echo -e "\nJAVA_HOME is set to an existing installation of Java: ${lcJavaHome}.\n"
fi
fi
echo -e "=> --------------------------------------"
echo "=> Saving Java Version ${lcVersion} Certificates"
echo -e "=> --------------------------------------\n"
# The below fixes problems with running maven with Java versions greater than 8. You'll get
# 'trustAnchors parameter must be non-empty' error messages as discussed here:
# https://stackoverflow.com/questions/39788295/maven-error-repeated-java-security-invalidalgorithmparameterexception-the-tr
# Here's a good explanation of the problem when running in Ubuntu
# https://bugs.launchpad.net/ubuntu/+source/ca-certificates-java/+bug/1739631
lcCerts="cacerts"
lcCertTopDir="/etc/ssl/certs/"
lcCertDirOriginal="${lcCertTopDir}java/"
lcCertDirSave="${lcCertTopDir}java.${lcVersion}/"
lcCertDirGet8="${lcCertTopDir}java.8/"
if [ ! -d "${lcCertDirSave}" ]
then
mkdir -p "${lcCertDirSave}"
fi
sudo cp "${lcCertDirOriginal}${lcCerts}" "${lcCertDirSave}${lcCerts}"
if [ "${lcVersion}" != "8" ]
then
echo -e "=> --------------------------------------"
echo "=> Retrieving Java Version 8 Certificates"
echo -e "=> --------------------------------------\n"
if [ ! -f "${lcCertDirGet8}${lcCerts}" ]
then
echo -e "${lcCertDirGet8}${lcCerts} does not exist. You should install Java 8 first, then Java ${lcVersion}"
else
echo -e "Running sudo cp ${lcCertDirGet8}${lcCerts} ${lcCertDirOriginal}${lcCerts}"
sudo cp "${lcCertDirGet8}${lcCerts}" "${lcCertDirOriginal}${lcCerts}"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment