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"
javaexecutable is called depends upon thePATHvariable, not theJAVA_HOMEvariable./usr/bin/javaon all macs, which is just a wrapper that goes elsewhere. Messing with PATH is not how you change that sort of thing.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.PATHis 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.