Skip to content

Instantly share code, notes, and snippets.

@octylFractal
Last active May 15, 2019 21:52
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save octylFractal/d85e0b160d8be75dbca29910a2b34f34 to your computer and use it in GitHub Desktop.
jpre -- Java Preloader

jpre

Java Preloader script. Invoke using jpre <version> to set the JAVA_HOME variable in your shell to a specific Java version, as specified in ~/.config/jpre/config.ini.

config.ini is an unprovided file. The format is simple, just add lines of the form <java version>=<path to java home>, e.g. 8=/usr/lib/jvm/openjdk-8.

To include the jpre command in your shell session, you must source the _java_preloader.sh script.

#!/usr/bin/env zsh
# Extract one key from the config file.
_jpre_java_home() {
grep "^$1=" "$HOME/.config/jpre/config.ini" | sed "s/$1=//" | head -n 1
}
jpre() {
[ -z "$1" ] && { unset JAVA_HOME; unset JPRE_JAVA_VERSION; printf '%s\n' 'Cleared JAVA_HOME.'; return 0; }
local java_version="$1"
local jhome="$(_jpre_java_home $java_version)"
[ -z "$jhome" ] && { printf '%s\n' "Unknown Java version '$java_version'." >&2; return 1; }
export JAVA_HOME="$jhome"
JPRE_JAVA_VERSION="$java_version"
printf '%s\n' "Now using Java $java_version."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment