Skip to content

Instantly share code, notes, and snippets.

@tobias
Created July 12, 2016 18:16
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save tobias/aab714378333e8a7bbc40f4f1d621147 to your computer and use it in GitHub Desktop.
Save tobias/aab714378333e8a7bbc40f4f1d621147 to your computer and use it in GitHub Desktop.
Manage multiple java versions on the mac from the fish shell
function setjdk
if test -n "$JAVA_HOME"
removeFromPath "$JAVA_HOME/bin"
end
set -gx JAVA_HOME (/usr/libexec/java_home -v $argv[1])
set -gx PATH $JAVA_HOME/bin $PATH
end
function removeFromPath
set -l idx 0
for x in (seq (count $PATH))
if test "$argv[1]" = "$PATH[$x]"
set idx $x
end
end
if test $idx -gt 0
set -e PATH[$idx]
end
end
@alonsoir
Copy link

alonsoir commented Apr 4, 2019

Thank you, you saved my day!

@nilsdjupvik
Copy link

THANK YOU!

@kparmar1
Copy link

kparmar1 commented Nov 9, 2019

Thanks, that was very helpful.

I added another gist to list the versions you have installed (v simple compliment to your gist). Ok maybe overkill but well i forget the command and you don't have to go look in the dirs.
https://gist.github.com/kparmar1/297681c3c002e16d15a1fb2d75a02324

@kparmar1
Copy link

kparmar1 commented Mar 5, 2022

FYI: I enhanced the script to be able to pick versions (with a -t flag) that might be the same but from different vendors as well (Oracle, OpenJDK ...) .
https://gist.github.com/kparmar1/9dbe7bfcb46d71393d4d59607a747b5b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment