Skip to content

Instantly share code, notes, and snippets.

@hferentschik
Created January 4, 2011 16:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hferentschik/765019 to your computer and use it in GitHub Desktop.
Save hferentschik/765019 to your computer and use it in GitHub Desktop.
Switched JDKs on Mac
J_VERSIONS_DIRECTORY="/System/Library/Frameworks/JavaVM.framework/Versions"
J_COMMANDS_SUBPATH="Commands"
J_HOME_SUBPATH="Home"
function availableJVMs()
{
ls -1 $J_VERSIONS_DIRECTORY | grep ^[0-9].[0-9]
}
function listJava()
{
local jvms=$(availableJVMs)
echo "Available JVMs: "$jvms
echo "Current Java:"
java -version
}
function setJava()
{
local target_jvm=""
local jvms=$(availableJVMs)
# Validate that the user requested an available JVM present on the system
for jvm in $jvms ; do
if [ "$jvm" == "$@" ]; then
target_jvm=$@
fi
done
if [ "$target_jvm" == "" ]; then
echo "Unsupported Java version requested"
return;
fi
# If we get here the user asked for a valid JVM, so configure it
echo "Configuring Shell Environment for Java "$@
# First unset any current set java, back to default before doing configuration
_unsetJava
# Generate the paths needed for the JVM requested
local jcmd="${J_VERSIONS_DIRECTORY}/$@/${J_COMMANDS_SUBPATH}"
local jhome="${J_VERSIONS_DIRECTORY}/$@/${J_HOME_SUBPATH}"
# We save the original path so we can toggle back if unset
ORIGINAL_PATH="$PATH"
PATH="$jcmd:${PATH}"
# We save the original JAVA_HOME so we can toggle back if unset
ORIGINAL_JAVA_HOME="$JAVA_HOME"
JAVA_HOME="$jhome"
# Update command prompt mode tag to note JVM setting
CURRENT_MODE_STRING="J$@"
echo "Current Java:"
java -version
}
function _unsetJava()
{
if [ "$CURRENT_MODE_STRING" != "" ]; then
PATH="$ORIGINAL_PATH"
JAVA_HOME="$ORIGINAL_JAVA_HOME"
CURRENT_MODE_STRING=""
fi
}
function unsetJava()
{
echo "Configuring Shell Environment for default Java"
_unsetJava
echo "Current Java:"
java -version
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment