I have Big Sur 11.2.1 from 18.02.2021. I had the same issue with JAVA_HOME path configuration. After reading a lot of information I solved my problem with next:
Install JDK 8: https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html Java SE Development Kit 8u281
Install it as usual on your MacOS Big Sur.
Check JDK version:
java -version
java version "1.8.0_281"
Java(TM) SE Runtime Environment (build 1.8.0_281-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.281-b09, mixed mode)
Check what versions of JDK are already installed on your machine:
/usr/libexec/java_home -V
Matching Java Virtual Machines (2):
1.8.281.09 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
1.8.0_281 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
Get only one that you need:
/usr/libexec/java_home -v 1.8.0_281
/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home
On Catalina and Big Sur - there is Z-shell , to add Environment variables like JAVA_HOME there are two files for that: ~/.zshenv and ~/.zshrc, I couldn't make it work with first one. It worked for me like this:
nano ~/.zshrc
Add to that file this line:
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0_281)
Press Ctrl+X and save changes.
Then run command to apply that changes to current terminal:
source ~/.zshrc
After that you can check if everything is working with command:
echo $JAVA_HOME
it will print you: /Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home
Restart your terminal and programs which will be using JAVA_HOME variable. For me it was Android Studio. Now everything works fine. I am new to Mac OS and it took me with 4hours to solve this. With Windows it's just done in a minute:)
Answer from yozhik on Stack Overflowjava - Wrong JAVA_HOME after upgrade to macOS Big Sur v11.0.1 - Stack Overflow
change java version in macOS BigSur - Stack Overflow
java - How to set JAVA_HOME environment variable on Mac OS X 10.9? - Stack Overflow
Bazel fails to recognize valid $JAVA_HOME on macOS Big Sur
Videos
I have Big Sur 11.2.1 from 18.02.2021. I had the same issue with JAVA_HOME path configuration. After reading a lot of information I solved my problem with next:
Install JDK 8: https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html Java SE Development Kit 8u281
Install it as usual on your MacOS Big Sur.
Check JDK version:
java -version
java version "1.8.0_281"
Java(TM) SE Runtime Environment (build 1.8.0_281-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.281-b09, mixed mode)
Check what versions of JDK are already installed on your machine:
/usr/libexec/java_home -V
Matching Java Virtual Machines (2):
1.8.281.09 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
1.8.0_281 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
Get only one that you need:
/usr/libexec/java_home -v 1.8.0_281
/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home
On Catalina and Big Sur - there is Z-shell , to add Environment variables like JAVA_HOME there are two files for that: ~/.zshenv and ~/.zshrc, I couldn't make it work with first one. It worked for me like this:
nano ~/.zshrc
Add to that file this line:
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0_281)
Press Ctrl+X and save changes.
Then run command to apply that changes to current terminal:
source ~/.zshrc
After that you can check if everything is working with command:
echo $JAVA_HOME
it will print you: /Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home
Restart your terminal and programs which will be using JAVA_HOME variable. For me it was Android Studio. Now everything works fine. I am new to Mac OS and it took me with 4hours to solve this. With Windows it's just done in a minute:)
Seems in macOS Big Sur v11.0.1 the behavior of the /usr/libexec/java_home -v ... command has changed: it is sensitive to the previously set value of JAVA_HOME environment variable.
Exact behavior is not clear, I couldn't find any documentation on this, but in my experiments it reported the version already set in JAVA_HOME, regardless of the -v switch:
% JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home /usr/libexec/java_home -v 1.8.0_162
/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
Additionally, I noticed that it reports nothing, if JAVA_HOME is set, but doesn't point to a valid java home (also for -V):
% JAVA_HOME=dummy /usr/libexec/java_home -v 1.7.0_45
% JAVA_HOME=dummy /usr/libexec/java_home -V
%
Solution is to ensure JAVA_HOME is not set before executing /usr/libexec/java_home:
% unset JAVA_HOME ; /usr/libexec/java_home -v 1.8.0_162
/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home
Please use below command :-
export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_261`
you have to unset JAVA_HOME and set JAVA_VERSION then it works.
~% JAVA_VERSION=12 java -version
openjdk version "14.0.2" 2020-07-14
~% export JAVA_VERSION=12
~% java -version
openjdk version "14.0.2" 2020-07-14
~% unset JAVA_HOME
~% java -version
java version "12.0.1" 2019-04-16
~% JAVA_VERSION=13 java -version
java version "13-ea" 2019-09-17
or if you need to set JAVA_HOME you need to unset before calling /usr/libexec/java_home
function jav {
unset JAVA_HOME
export JAVA_HOME=`/usr/libexec/java_home -v $@`
}
~% jav 12
~% java -version
java version "12.0.1" 2019-04-16
~% jav 13
~% java -version
java version "13-ea" 2019-09-17
If you're using bash, all you have to do is:
echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile
If you're using zsh (which probably means you're running macOS Catalina or newer), then it should instead be:
echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.zshrc
In either case, restart your shell.
If you have multiple JDK versions installed and you want it to be a specific one, you can use the -v flag to java_home like so:
echo export "JAVA_HOME=\$(/usr/libexec/java_home -v 1.7)" >> ~/.bash_profile
I just spent 2 hours setting this variable. The other answers did not work properly for me. I'm using macOS Catalina 10.15.4.
First, find your actual Java SDK Home directory:
/usr/libexec/java_home
Manually navigate there to make sure you don't have any mistakes due to incorrect versions, etc. For me, this was:
/Library/Java/JavaVirtualMachines/jdk-13.0.2.jdk/Contents/Home
Next, edit your terminal's profile. If you're using zsh, this will be:
vim ~/.zshrc
If you're not using zsh, this will be:
vim ~/.bash_profile
Inside, add the following new line anywhere in the file:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-13.0.2.jdk/Contents/Home
Restart your terminal app (or source ~/.bash_profile), and it should work properly.