3

I am having trouble changing the version of java running on my Mac running Big Sur 11.2 with zsh. I want to set Java 1.8 as my default, so I used the following commands to set JAVA_HOME:

$ unset JAVA_HOME $ export JAVA_HOME=$(/usr/libexec/java_home -v "1.8.0_281") $ echo $JAVA_HOME /Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home 

However, when I ran java -version I kept getting JDK 15 as the current default. I then removed jdk15 from the JavaVirtualMachines directory and repeated the setting of JAVA_HOME, but I continue to observe the same result.

6
  • 1
    Which java executable is called depends upon the PATH variable, not the JAVA_HOME variable. Commented Feb 17, 2021 at 21:06
  • @PiotrP.Karwasz ... and that is /usr/bin/java on all macs, which is just a wrapper that goes elsewhere. Messing with PATH is not how you change that sort of thing. Commented Feb 17, 2021 at 21:14
  • @rzwitserloot .. which is probably a symbolic link to the real executable. Commented Feb 17, 2021 at 21:17
  • 1
    @PiotrP.Karwasz Indeed it is, and that file cannot be overwritten or changed in any way, not even with sudo. The only way to do it, is to disable system security which is quite the ordeal and extremely not recommended - that's absolutely not the way to change mac defaults. I know it's weird - there is no point trying to assist with this question if you are not familiar with how macs work. Commented Feb 17, 2021 at 21:28
  • 2
    @rzwitserloot you mean “PATH is supposed to contain /usr/bin/java (and no other java)”. You don’t really know, what it contains on the OP’s machine. On the other hand, we do know that something in the OP’s environment doesn’t work as it is supposed to do. Commented Feb 18, 2021 at 7:42

3 Answers 3

4

Other answers so far are talking about $PATH. This is a red herring; /usr/bin/java is on the path, it should remain on the path, and it is a light wrapper that goes to your actual, selected java installation.

The recommended strategy is to just use tools to solve this problem for you, such as jEnv. Install it via brew if you have it, otherwise, follow instructions on the site.

If you don't want to go the established route of using jEnv, the not-recommended route is, indeed, to mess with JAVA_HOME. /usr/bin/java will use whatever default rolls out of /usr/libexec/java_home. java_home will defer to what JAVA_HOME (the environment variable) says; only if it is unset will it give you its own default, which is generally the latest installed and very hard to change otherwise.

The fact that it isn't working for you suggests you have some different kind of shell, or that java version isn't listed by java_home, or you've messed with PATH in some very drastic ways (would involve removing security control from your mac disk and such), so I doubt that's it - doublecheck what you've done so far? Which mac version are you on?

An example from a mac:

/usr/libexec/java_home > /Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home java -version > openjdk version "14.0.1" 2020-04-14 /usr/libexec/java_home -v 1.8 > /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home /usr/libexec/java_home > /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home java -version > openjdk version "1.8.0_252" 
Sign up to request clarification or add additional context in comments.

Comments

0

You should ensure that the right java version path come first in your PATH environmental variable

1 Comment

/usr/bin/java is on the path, and that is a wrapper that goes to an actual java installed elsewhere. Messing with the PATH is not how you change this stuff.
0

TLDR:

  1. echo $PATH and copy it's value
  2. export PATH=""
  3. export PATH="/path/from/step1/with/java/removed"

End TLDR

Had the same issue with Big Sur when I tried to switch to java 11, despite everything pointing to correct values, java -version always returned java 8

$ java -version openjdk version "1.8.0_292" OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_292-b10) OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.292-b10, mixed mode) $ whereis java /usr/bin/java $ /usr/bin/java -version openjdk version "11.0.11" 2021-04-20 OpenJDK Runtime Environment AdoptOpenJDK-11.0.11+9 (build 11.0.11+9) OpenJDK 64-Bit Server VM AdoptOpenJDK-11.0.11+9 (build 11.0.11+9, mixed mode) $ echo $JAVA_HOME /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home $ java -version openjdk version "1.8.0_292" OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_292-b10) OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.292-b10, mixed mode) $ which java /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/bin/java 

To fix I had to remove Java SDK from PATH using steps in TLDR above

$ echo $PATH /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/opt/X11/bin:/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home/bin:/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/bin:/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home/bin $ export PATH="" direnv: error can't find bash: exec: "bash": executable file not found in $PATH $ export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/opt/X11/bin" $ echo $PATH /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/opt/X11/bin 

Afterwards

$ java -version openjdk version "11.0.11" 2021-04-20 OpenJDK Runtime Environment AdoptOpenJDK-11.0.11+9 (build 11.0.11+9) OpenJDK 64-Bit Server VM AdoptOpenJDK-11.0.11+9 (build 11.0.11+9, mixed mode) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.