MacOS Flutter JAVA_HOME Issue with Android Studio

If you have JAVA_HOME issue with below message on MacOS please follow my steps.

You can get below messages when you run flutter command on terminal.

"ERROR: JAVA_HOME is set to an invalid directory: /usr/bin/java"

or

"Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."

or

"ERROR: JAVA_HOME is not set and no ‘java’ command could be found in your flutter PATH. in flutter"

For example I got this error when I try to accept android licenses with “–android-licenses” parameter.

% flutter doctor -v
[✓] Flutter (Channel stable, 2.5.2, on macOS 11.6 20G165 darwin-arm, locale
en-TR)
• Flutter version 2.5.2 at /Users/?your_username?/Development/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 3595343e20 (35 hours ago), 2021-09-30 12:58:18 -0700
• Engine revision 6ac856380f
• Dart version 2.14.3

[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at /Users/?your_username?/Library/Android/sdk
• Platform android-30, build-tools 30.0.3
• Java binary at: /Applications/Android
Studio.app/Contents/jre/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)
✗ Android license status unknown.
Run `flutter doctor --android-licenses` to accept the SDK licenses.
See https://flutter.dev/docs/get-started/install/macos#android-setup for
more details.

[✓] Xcode - develop for iOS and macOS
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 13.0, Build version 13A233
• CocoaPods version 1.10.2

[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio
• Android Studio at /Applications/Android Studio Preview.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[✓] Android Studio (version 2020.3)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)

[✓] VS Code (version 1.60.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.26.0

[✓] VS Code (version 1.60.0-insider)
• VS Code at /Applications/Visual Studio Code - Insiders.app/Contents
• Flutter extension version 3.25.0

[✓] Connected device (1 available)
• Chrome (web) • chrome • web-javascript • Google Chrome 94.0.4606.71

! Doctor found issues in 1 category.

You can get this JAVA_HOME traditional and annoying error message on different stages.

In past time I have solved this problem with symlink but it is not persistent.

I think, I have solved this problem with following steps permanently

1.) Run “flutter config –android-studio-dir=/Applications/Android\ Studio”

2.) On terminal window, try to learn your jdk path. You can look at “/Library/Java/JavaVirtualMachines” directory.
In my example my directory is “jdk-16.jdk”

3.) Run [export JAVA_HOME=”/Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home”]

If you want to set this environment variable to persistent path you can follow sub steps.

3-a) Run “nano $HOME/.zhrc”

3-b) Add this line to the file “export JAVA_HOME=”/Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home”

3-c) Run source $HOME/.zshrc on terminal window.

After these steps you can run –android-licenses command without get any error message.