Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java -version does not change

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.

like image 879
Alam Figueroa Avatar asked May 20 '26 07:05

Alam Figueroa


1 Answers

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"
like image 95
rzwitserloot Avatar answered May 22 '26 22:05

rzwitserloot