Skip to content

Instantly share code, notes, and snippets.

@aphor
Created January 4, 2019 21:46
Show Gist options
  • Save aphor/2574f57d3ca75a4045b230bbc26882ea to your computer and use it in GitHub Desktop.
Save aphor/2574f57d3ca75a4045b230bbc26882ea to your computer and use it in GitHub Desktop.
Mac shell java switching
#!/bin/bash
# Put this .javapath file in your home directory with execute permission.
# Run it to create java dotfiles in your home directory to set up
# PATH and JAVA_HOME for indicated java distros
# Source the resulting dotfiles to switch current JAVA
if (return 2>/dev/null)
then
PATH=$(echo "$PATH" |
awk -F: '{for (i=1;i<=NF;i++){print $i}}' |
egrep -v 'j(dk|re)' |
( echo "$JAVA_HOME/bin"; cat ) |
awk 'BEGIN {d=""} {printf("%s%s",d,$0);d=":"} END{printf "\n"}')
export PATH
echo "PATH=$PATH"
else
jvms_root="/Library/Java/JavaVirtualMachines"
cd "${jvms_root}"
ls |
(while read java
do
export java
rcfile="${HOME}/.$( echo $java|sed -E '/\.j(re|dk)/s///')"
(echo 'export JAVA_HOME='"$(find ${jvms_root}/${java} -name Home|head -1)";
echo ". ~/.javapath";echo) > "${rcfile}"
chmod ug+x "${rcfile}" && echo "${rcfile}"
done)
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment