2

I am trying to change the default Java version on my machine from version 15 (i.e. openjdk 15.0.1) to version 11 (i.e. openjdk version 11.0.2). I have followed various solutions provided online, but at the end of the day, I just want the following result:

$ java --version
openjdk 11.0.9 2020-10-20
OpenJDK Runtime Environment (build 11.0.9+9)
OpenJDK 64-Bit Server VM (build 11.0.9+9, mixed mode, sharing)

But instead, I get:

$ java --version
openjdk 15.0.1 2020-10-20
OpenJDK Runtime Environment (build 15.0.1+9)
OpenJDK 64-Bit Server VM (build 15.0.1+9, mixed mode, sharing)

Most solutions that I've found, suggest setting a new value to my JAVA HOME environmental variable (both in-line and in .bash_profile), where as of now, I have as:

$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk-11.0.9.jdk/Contents/Home

Even removing jdk-15.0.1.jdk entirely from /Library/Java/JavaVirtualMachines/ didn't seem to change anything. So, this JDK must be defined and used somewhere else, and its existance is overriding anything I put in my .bash_profile file. Has anyone else experienced this before? Is this change possible?

Chloe Bennett
  • 901
  • 2
  • 10
  • 23

2 Answers2

1

It turns out that the issue was within my .bash_profile. While I'm not sure if this is the case for everyone, my PATH environment variable settings superseded my JAVA_HOME environment variable, and my PATH variable had reference my JAVA_HOME variable inside it. This was resolved by changing the following in my .bash_profile file.

If JAVA_HOME is the only variable added, then the following should work.

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.9.jdk/Contents/Home

export PATH=$JAVA_HOME/bin

If there are others then this should work.

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.9.jdk/Contents/Home

export SOME_OTHER_PATH=/somepath

export PATH=$SOME_OTHER_PATH:$JAVA_HOME/bin

Thanks everyone for their help!

Chloe Bennett
  • 901
  • 2
  • 10
  • 23
0

Make sure that the first line in your .bash_profile file that talks about JAVA_HOME is this:

export JAVA_HOME=$(/usr/libexec/java_home -v 11.0.9)

You can run /usr/libexec/java_home -V to see a list of all your installed Java Virtual Machines. On my machine, I have the following (mine will look slightly different because this output is from Big Sur):

Matching Java Virtual Machines (6):
    15.0.1 (x86_64) "UNDEFINED" - "OpenJDK 15.0.1" /usr/local/Cellar/openjdk/15.0.1/libexec/openjdk.jdk/Contents/Home
    11.0.9 (x86_64) "UNDEFINED" - "OpenJDK 11.0.9" /usr/local/Cellar/openjdk@11/11.0.9/libexec/openjdk.jdk/Contents/Home
    11.0.6 (x86_64) "Oracle Corporation" - "Java SE 11.0.6" /Library/Java/JavaVirtualMachines/jdk-11.0.6.jdk/Contents/Home
    11.0.1 (x86_64) "Oracle Corporation" - "Java SE 11.0.1" /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
    1.8.0_275 (x86_64) "UNDEFINED" - "OpenJDK 8" /usr/local/Cellar/openjdk@8/1.8.0+275/libexec/openjdk.jdk/Contents/Home
    1.8.0_131 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
/usr/local/Cellar/openjdk@8/1.8.0+275/libexec/openjdk.jdk/Contents/Home

The line at the very bottom (which may not exist in Catalina, but which you can get by running /usr/libexec/java_home is your currently selected JVM.

Sam
  • 2,350
  • 1
  • 11
  • 22
  • But this just sets JAVA_HOME, RIGHT? The OP said they tried that, and it didn't change anything...if I'm reading correctly. – CryptoFool Dec 10 '20 at 23:38
  • @Steve yes, it does. I suspect that the issue is still with `$JAVA_HOME` (having dealt with a lot of problems with it myself), unless I'm misunderstanding the question, in which case the question you linked seems like the best bet. – Sam Dec 10 '20 at 23:44
  • Hi @Sam, this is the very first line in my `.bash_profile` already. I've tried quick a few solutions, but I'm not sure what is happening. It almost seems as though it is ignoring the file altogether. – Chloe Bennett Dec 10 '20 at 23:46
  • @ChloeBennett OK, just to make sure the file works, add the line `echo "Profile Test"` to your `.bash_profile` and then open a new shell/source your profile. "Profile Test" should be printed right above your prompt. Does that work? – Sam Dec 10 '20 at 23:51
  • That said, I've found the issue, and I don't think I would have noticed it without this thread. It turns out that my overarching PATH variable (which was initially using my JAVA_HOME variable, had been overwriting by my android settings). So, my system was reading PATH, but not JAVA_HOME. – Chloe Bennett Dec 11 '20 at 00:39
  • @ChloeBennett Good to hear! – Sam Dec 11 '20 at 00:40