Last active
August 29, 2015 14:02
-
-
Save cristobal/9320e000a3d33fe360fb to your computer and use it in GitHub Desktop.
Change Java HOME runtime on OS X
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # | |
| # Resources: | |
| # - http://superuser.com/questions/490425/how-do-i-switch-between-java-7-and-java-6-on-os-x-10-8-2 | |
| # | |
| JAVA_VMS="/Library/Java/JavaVirtualMachines" | |
| ####################### | |
| # List available jdks # | |
| ###################### | |
| function list_versions { | |
| echo "Available jdk versions are:" | |
| for version in $(ls -1 ${JAVA_VMS} | sed 's/.jdk//g' | sed 's/jdk//g' | tr "\n" " "); do | |
| echo " ${version}" | |
| done | |
| } | |
| ###################### | |
| # Use a specific jdk # | |
| ##################### | |
| function use_version { | |
| version="jdk${1}.jdk" | |
| if [[ "$(ls -1 ${JAVA_VMS} | grep -E "${version}" | wc -l )" -eq 0 ]]; then | |
| echo "No such version \`$1\` in the JavaVirtualMachines directory" | |
| return | |
| fi | |
| path=$PATH | |
| if [[ "$(echo $PATH | grep -E "${JAVA_VMS}" | wc -l | awk '{print $1}')" -eq 1 ]]; then | |
| path=$(echo $PATH | tr ":" "\n" | grep -v "${JAVA_VMS}" | tr "\n" ":") | |
| fi | |
| # remove trailing ":" | |
| if [ "${path:(${#path}-1):1}" = ":" ]; then | |
| path=${path%?} | |
| fi | |
| # setup java paths | |
| java_home_dir="${JAVA_VMS}/${version}/Contents/Home" | |
| java_bin_dir="${java_home_dir}/bin" | |
| # /usr/libexec/java_home -v $1 --exec | |
| export JAVA_HOME=$java_home_dir | |
| export PATH=$JAVA_HOME/bin:$path | |
| cat <<EOF | |
| Updated \$JAVA_HOME and \$PATH to following values: | |
| JAVA_HOME=$JAVA_HOME | |
| PATH=$PATH" | |
| EOF | |
| } | |
| function remove_version { | |
| version="jdk${1}.jdk" | |
| if [[ "$(ls -1 ${JAVA_VMS} | grep -E "${version}" | wc -l )" -eq 0 ]]; then | |
| echo "No such version \`$1\` in the JavaVirtualMachines directory" | |
| return | |
| fi | |
| dir="${JAVA_VMS}/${version}" | |
| echo "Attempting to remove the jdk version $1" | |
| sudo rm -rf $dir | |
| } | |
| ################ | |
| # Print usage # | |
| ############### | |
| function print_usage { | |
| # run as jvm bash alias with . | |
| arg=$0 | |
| if [[ "$arg" = "-bash" ]]; then | |
| arg="jvm" | |
| fi | |
| cat <<EOF | |
| Usage: $(basename $arg) OPTION [\$ARG ...] | |
| OPTIONS | |
| list List available versions | |
| remove \$VERSION Remove the specified version | |
| use \$VERSION Specify the version to use | |
| help Print this help | |
| EOF | |
| } | |
| ################### | |
| # Read user input # | |
| ################### | |
| function read_user_input { | |
| for el in "$@"; do | |
| if [[ "$el" = "list" ]]; then | |
| list_versions | |
| return | |
| elif [[ "$el" = "remove" ]]; then | |
| remove_version $2 | |
| return | |
| elif [[ "$el" = "use" ]]; then | |
| use_version $2 | |
| return | |
| elif [[ "$el" = "help" ]]; then | |
| print_usage $@ | |
| return | |
| fi | |
| done | |
| print_usage | |
| } | |
| read_user_input "$@" |
Author
Author
Added "remove" function.
Creating a bash alias i.e.
alias jvm=". jvm.sh"
Comes in handy and no need source . the script manually.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Running the script
You will need to source the script when running:
or
To be able to export the values from the script to the current user environment.
These changes are not permanent but only available for the current user environment.
Available commands:
listList all available jdk versions from the JavaVirtualMachines directoryuseThe version to useExpected requirements
An OS X environment