Skip to content

Instantly share code, notes, and snippets.

@jeremywall
Last active February 27, 2024 17:46
Show Gist options
  • Save jeremywall/af9d06e7e16fbafec1a3c2f90a672449 to your computer and use it in GitHub Desktop.
Save jeremywall/af9d06e7e16fbafec1a3c2f90a672449 to your computer and use it in GitHub Desktop.
A bash script to attempt to locate the computer's default JVM and update it's time zone database file
#!/bin/bash
# use of this script means you agree to hold harmless and indemnify the author of the script
# use at your own risk
# usage example: update_jmv_tzdb.sh 2024a
if [ $# -eq 0 ]; then
echo "TZDB version argument missing"
exit
fi
if [ -z "$1" ]; then
echo "TZDB version argument empty"
exit
fi
# locate the tzdb.dat file
JAVA_EXE_PATH="`readlink -f /usr/bin/java`"
JAVA_JRE_BIN_DIR_PATH="`dirname ${JAVA_EXE_PATH}`"
JAVA_JRE_LIB_DIR_PATH="`dirname ${JAVA_JRE_BIN_DIR_PATH}`/lib"
JAVA_TZDB_PATH="${JAVA_JRE_LIB_DIR_PATH}/tzdb.dat"
# if tzdb.dat is not found exit
if [ ! -f "$JAVA_TZDB_PATH" ]; then
echo "Unable to locate existing TZDB ${JAVA_TZDB_PATH}"
exit
fi
# get the version of the existing tzdb.dat
TZDB_OLD_VERSION=`head -c 16 ${JAVA_TZDB_PATH} | tail -c 5`
echo "Located existing TZDB ${TZDB_OLD_VERSION} ${JAVA_TZDB_PATH}"
# download new tzdb.dat file based on arguments
TZDB_NEW_VERSION="${1}"
TZDB_DOWNLOAD_URL="https://raw.githubusercontent.com/jeremywall/java-tzdb-builder/main/releases/${TZDB_NEW_VERSION}/tzdb${TZDB_NEW_VERSION}.dat"
HTTP_RESPONSE_CODE=`curl -O -f -w "%{http_code}" "${TZDB_DOWNLOAD_URL}"`
echo "Download TZDB ${TZDB_NEW_VERSION} HTTP Response ${HTTP_RESPONSE_CODE}"
# if not HTTP 200 then exit
if [ "200" != "${HTTP_RESPONSE_CODE}" ]; then
echo "Failed to download TZDB ${TZDB_NEW_VERSION} from ${TZDB_DOWNLOAD_URL}"
exit
fi
# backup existing tzdb.dat
cp "${JAVA_TZDB_PATH}" "${JAVA_TZDB_PATH}.${TZDB_OLD_VERSION}.bak"
echo "Backed up existing TZDB to ${JAVA_TZDB_PATH}.${TZDB_OLD_VERSION}.bak"
# copy new tzdb.dat into place
cp "tzdb${TZDB_NEW_VERSION}.dat" "${JAVA_TZDB_PATH}"
echo "Copied new TZDB to ${JAVA_TZDB_PATH}"
# set permissions on tzdb.dat
chmod 644 "${JAVA_TZDB_PATH}"
echo "Setting TZDB file permissions to 644"
echo "Now you need to restart all Java applications on this server"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment