Skip to content

Instantly share code, notes, and snippets.

@StFS
Last active May 15, 2018 09:57
Show Gist options
  • Save StFS/49d95f87de644510e7b5fa4575709e72 to your computer and use it in GitHub Desktop.
Save StFS/49d95f87de644510e7b5fa4575709e72 to your computer and use it in GitHub Desktop.
oh-my-zsh plugin that sets the JAVA_HOME and path to activate a specified version of a JDK (installed via webupd8 packages). Install in .oh-my-zsh/custom/plugins/use-java/use-java.plugin.zsh
function _use-java_usage() {
echo "Usage use-java VERSION"
echo 'Set the Oracle JDK version to use for the current session.'
echo ''
echo 'The VERSION is the JDK version to use such that'
echo 'the directory /usr/lib/jvm/java-${VERSION}-oracle'
echo 'will be set as JAVA_HOME and the corresponding bin/'
echo 'directory will be added to the front of PATH.'
}
function use-java() {
if [[ -z "$1" ]]; then
_use-java_usage
return -1
fi
local JVM_DIR="/usr/lib/jvm/java-$1-oracle"
if [[ -d ${JVM_DIR} ]]; then
echo "Setting session JDK to ${JVM_DIR}"
export JAVA_HOME=${JVM_DIR}
export PATH=${JVM_DIR}/bin:${PATH}
java -version
else
echo "No Oracle JDK found at ${JVM_DIR}"
return -1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment