Search code examples
javabashmacosjava-home

Can't set JAVA_HOME dynamically on macOS Catalina


tl,dr; Doing export JAVA_HOME=$(/usr/libexec/java_home -v 14) once JAVA_HOME was already set won't change active java


Steps

  1. set JAVA_HOME first (for example with export JAVA_HOME=$(/usr/libexec/java_home -v 11)):
$ echo ${JAVA_HOME}
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home

$ java -version
openjdk version "11.0.8" 2020-07-14
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.8+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.8+10, mixed mode)
  1. Try to set it to different version (for example export JAVA_HOME=$(/usr/libexec/java_home -v 14)) - it still uses previously set java (even though JAVA_HOME was updated):
$ echo ${JAVA_HOME}
/Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home

$ java -version
openjdk version "11.0.8" 2020-07-14
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.8+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.8+10, mixed mode)

I'm not sure why it's not working now - it was working just fine a while back (I'd say 2-3 months ago)

There is a seemingly similar topic (Can't set JAVA_HOME on Catalina) but it boils down to different shell. In my case I'm using same shell - bash from brew:

$ bash -version
GNU bash, version 5.0.18(1)-release (x86_64-apple-darwin19.5.0)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Solution

  • OK, I finally managed to solve the issue. As other have pointed out, everything has to do with PATH variable. And I indeed had JAVA_HOME/bin in there… which causes the problem. Today during an update I notice that openjdk brew formula had this comment:

    keg_only "it shadows the macOS `java` wrapper"
    

    And things immediately "clicked". After searching a bit I found Why isn't java wrapper not picking up the right version specified in JAVA_HOME on macOS? which confirmed the issue. By including JAVA_HOME in the PATH in my .profile file I was breaking native macOS functionality of it's java wrapper, which selects active java version based on currently set JAVA_HOME.

    This is my example session (from opening the shell and having export JAVA_HOME=$(/usr/libexec/java_home -v 11) in the .profile file) -- I have default from the profile, then I set explicitly JAVA_HOME and it switches java version on the fly:

    $ java -version
    openjdk version "11.0.8" 2020-07-14
    OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.8+10)
    OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.8+10, mixed mode)
    
    $ export JAVA_HOME=$(/usr/libexec/java_home -v 14)
    
    $ java -version
    openjdk version "14.0.2" 2020-07-14
    OpenJDK Runtime Environment AdoptOpenJDK (build 14.0.2+12)
    OpenJDK 64-Bit Server VM AdoptOpenJDK (build 14.0.2+12, mixed mode, sharing)