How to switch JDK version on Mac OS X

Apple decided to remove the Java Preferences application in a software update and afterwards in has been difficult to change the JDK version on Mac OS X. I especially noticed this after I installed the JDK 8 early access preview which makes some of the tips that you find on various blogs online useless. After a lot of googling I finally found a simple solution that I’d like to share.

What you do is to edit your ~/.bash_profile and add the following:

What the script does is to first remove other JDK versions in the PATH so that they won’t interfere with our new JDK version. Then it makes some clever use of /usr/libexec/java_home which is a command that lists installed JDK versions. The -v argument tells java_home to return the path of the JDK with the supplied version, for example 1.7. We also update the PATH to point to the bin directory of the newly found JAVA_HOME directory. At the end we can simply execute the function using

which selects the latest installed JDK version of the 1.7 branch. To select a specific version you can simply execute

instead. Run /usr/libexec/java_home -h to get more details on how to choose versions.

That’s it! All credits should go to Neeme Praks and his answer on


  1. Nelson

    Thanx for the script. Worked well for me.

  2. Joaquín

    Thanks man!

    I was running something like export JAVA_HOME=java_home -v 1.6, but the scripts also takes care of the PATH so it’s definitively more convenient.

    For those which, like in my case, don’t have ~/.bash_profile, you can also add it like an script under /usr/local/bin

  3. Günter

    Many thanks!

    Works really great for Java up to version 1.8

  4. That was really helpful.

  5. Thank you very much for yet another very elegant solution to an annoying problem!

  6. George

    Works well but borks on the removeFromPath function when your Path contains a folder that contains spaces, for example “/Applications/Flash Player”.

  7. venu

    Thank you so much!

  8. Richard

    Great. Works perfectly well and solve a really annoying issue :)

  9. Nice one.. thanks!

  10. Margaret

    Thank you thank you, this saved my butt! I can’t believe Apple removed feature to change it trivially though :(

  11. Jitendra Vyas

    I’m getting this message in terminal after adding you code to .bash_profile

    Unable to find any JVMs matching version “1.7”.

  12. Yakov Fain

    This works for applications, but not for applets. I need my Firefox to use Java 1.7 plugin, but after running your script FIrefox still has Java 1.8 plugin and so far I don’t know how to make it switch to Java 1.7.

    Thanks for the script anyway!

  13. John Van Der Loo

    Thanks for this! Most helpful in managing multiple JDK versions.

  14. Niklas Modin

    This should be added to .bash_profile as part of JDK installation.
    Or perhaps burnt into an EPROM on every new Mac sold.

    Anyways, thank you for making my day suck a lot less !

  15. Ragunath Jawahar

    Life saver!

  16. Tyler

    Super simple and straight forward! Thank you so much!

  17. Jeff

    Great! Well-explained and just what I was looking for.

  18. Nicolas U.

    Time saver ! Thanks for sharing this.

  19. Fabio Quimbay

    Thank you Johan, the script works perfectly !!

  20. Chris

    I refined the script just a little bit to be able to choose from the existing versions, which saves entering the java_home -V:

    function setjdk() {
    if [ $# -ne 0 ]; then
    switchjdk $@
    echo “Choose from the following Java versions:”
    echo “Please enter the correct full version number:”
    read jdkPick
    switchjdk $jdkPick

    function switchjdk() {
    removeFromPath ‘/System/Library/Frameworks/JavaVM.framework/Home/bin’
    if [ -n “${JAVA_HOME+x}” ]; then
    removeFromPath $JAVA_HOME
    export JAVA_HOME=/usr/libexec/java_home -v $@
    export PATH=$JAVA_HOME/bin:$PATH

    function choosejdk() {
    allVersions=$(/usr/libexec/java_home -V)

    function removeFromPath() {
    export PATH=$(echo $PATH | sed -E -e “s;:$1;;” -e “s;$1:?;;”)

  21. Sujesh Chirackkal

    Thanks for this. Really helpful

  22. Torben Pedersen

    Works – nice, thanks.

  23. Mike

    SO awesome! Thanks for reposting this.

  24. This will work as well, and is relatively robust assuming ‘tr’ and ‘sed’ behave nicely (uses bash):

  25. Martin Kretz

    I think JEnv ( solves the problem nicely, with added bonus of customizing java version for each directory.

  26. Ajay Singh

    Thanks! Works like a charm.

  27. Paul

    This was AWESOME. Thanks!

  28. Jessica

    Thanks for this function, really useful !

    However, I have a question : I need 2 versions (jdk 1.8 for maven and 1.7 for jboss).
    To switch between my 2 servers, I have to add or remove your function. Is there a solution to do have something who recognize automatically which version I need ? To avoid editing each time the bash profile ?
    Thanks in advance !

  29. Elias

    Thank you tons!!!

  30. Trey

    I think that line 5 should be: “removeFromPath $JAVA_HOME/bin”; otherwise, it leaves behind a stray “/bin:” each time you use setjdk. Except for that, it works great! I only noticed it because I changed the PATH after the call to setjdk and that borked the directory that I’d added.

  31. Anton

    THANKS – great help!!!

  32. Mike N

    Thanks. I’m using it with latest version of 1.8.

  33. Krupa Singavarapu

    Great this script is working for me.

  34. Ranky

    Very nice. Thanks.

  35. Harald

    Thank you very much! Nevertheless, i.m.h.o. this approach is much easier and more flexible:

    Or is there a problem I don’t recognize? Thanks again.

  36. Paul Hilliar

    Awesome, awesome, awesome and awesome. Thanks for a great script!

  37. Brett Tofel

    very nice

  38. Just want to thank you for this very useful tip !
    Works great when switching between 7u60 and 8u40

  39. Sohil


    Thank you buddy.

  40. Steve Coffman

    I noticed that the removeFromPath was leaving the trailing /bin in the PATH. I corrected it (and made it pass shellcheck linter).


    function setjdk() {
    if [ $# -ne 0 ]; then
    removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
    #Bash expansion will be "x" if JAVA_HOME set, emptystring if unset
    if [ -n "${JAVA_HOME+x}" ]; then
    removeJavaFromPath "${JAVA_HOME}/bin"
    export JAVA_HOME=$(/usr/libexec/java_home -v "$@")
    export PATH=$JAVA_HOME/bin:$PATH
    function removeJavaFromPath() {
    export PATH="$(echo "$PATH" | sed -E -e "s;:$1;;" -e "s;$1:?;;")"

  41. Steve Coffman

    Ok, the wordpress engine mangled the bash script where I corrected the original’s path bug, and I made it a little more user friendly.

    Here’s a clean version:

  42. Torbjorn Martinsen

    There is a different answer in the SuperUser question you linked with more upvotes. I ended up going for that solution – perhaps consider updating your article if you also like that solution better as your article was the top hit on google for me :)

  43. Sreedish

    That was superb!!! thanks a lot for solving an annoying problem

  44. Tunde

    I’m getting this error when I ran the file:

    Babatundes-iMac:~ tfalade$ source ~/.bash_profile
    -bash: /Users/tfalade/.bash_profile: line 3: syntax error near unexpected token then'
    -bash: /Users/tfalade/.bash_profile: line 3:
      if [ $# -ne 0 ]; then’

    Is there something that I’m doing wrong?

  45. Tunde

    I’m getting this error when I ran the function:
    Babatundes-iMac:~ tfalade$ source ~/.bash_profile
    -bash: /Users/tfalade/.bash_profile: line 3: syntax error near unexpected token then'
    -bash: /Users/tfalade/.bash_profile: line 3:
      if[ $# -ne 0 ]; then’

    Please help

  46. Josef

    Awesome, works like a charm! I forgot about changing the PATH, I typically just jet JAVA_HOME, but that stopped working recently (in some cases), since there very symlinks like /usr/bin/java ->

  47. Kostas Karkaletsis

    Thanks for the script. Works as it is described!

  48. Lyndon

    Many thanks. Works a treat!!

  49. vito

    Thanks ! Work great !

  50. KaelLuo

    Thanks for this, it work for me.
    Just setjdk 1.7 should not include in script

  51. Ali BAKAN

    thnx a lot.

  52. Stan

    Working great in 2018 on Sierra!

Trackbacks for this post

  1. JDK setting on Mac OS X Maverick causing you pain? | Java Ninja Chronicles By Norris Shelton, Jr
  2. Why Java Applets should be banned | Yakov Fain's Blog
  3. Apache Maven & Homebrew - GeekApproach
  4. Java 8 on OSX 10.9 - Mac-Forums Discussions for Apple Products & Services
  5. 1 – Switch JDK version on OS X |
  6. Grunt, Karma, Webpack und Co. - Toolchains für JavaScript-Projekte - codecentric Blog : codecentric Blog

Leave a Reply