'mvn not able to get JAVA_HOME in mac

I am using macOs And I have set JAVA_HOME in ~/.zshrc file, Even I have checked the echo $JAVA_HOME and its showing correct path.

macOs version: Monterey After this mvn is giving error.(maven version 3.8.4)

yogendrasingh@197NODMB29073 code % mvn -version
The JAVA_HOME environment variable is not defined correctly,
this environment variable is needed to run this program.


Solution 1:[1]

Do you have other versions of Java and Maven installed? Maybe try reinstalling both of them.

Solution 2:[2]

Try exporting JAVA_HOME like this in your ~/.zshrc file:

export JAVA_HOME=$(/usr/libexec/java_home)

And don't forget to execute source ~/.zshrc or to restart your shell afterwards

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Knacker777
Solution 2 oemel09