Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter not using jdk from JAVA_HOME

I am currently developing a Flutter application on my Mac and am trying to test my app on an Android emulator. To facilitate this, I have installed Android Studio along with the Android SDK. Running flutter doctor --verbose shows that everything has been set up correctly.(no warings or errors)

However, I am encountering an issue related to Java versions. When I attempt to start my app for Android, I receive an error stating that my Gradle version (Gradle-6.9.3, which I prefer not to change) is incompatible with my JDK. According to the error message, my system needs to use Java 15 to achieve compatibility.

When I run java --version, the output indicates that I am indeed using Java 15. Furthermore, I've set my $JAVA_HOME environment variable to the output of /usr/libexec/java_home -v 15, so it should be referencing the correct Java version.

However, the issue persists. When I execute flutter doctor --verbose, it returns Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694). This suggests that Flutter is using Java 17 instead of Java 15, which is referenced by $JAVA_HOME.

My hypothesis is that either Flutter or Android Studio might be using their own bundled JDK, which seems to override my system settings. I am looking for a way to change this behavior so that Flutter uses the JDK version specified in $JAVA_HOME.

In an attempt to solve this issue, I have installed Java 15 and configured the $JAVA_HOME environment variable accordingly. Additionally, I modified the Flutter command so that it sets the correct $JAVA_HOME value right before executing Flutter.

Despite these adjustments, it seems that Flutter is still ignoring the $JAVA_HOME variable and opting to use the bundled Java version instead. This is problematic because I need to use Java 15 for compatibility with other tools and libraries in my project.

I considered the possibility of completely removing Java 17 from my system as a potential solution. However, when I ran sudo /usr/bin/find / -name "*.jdk" to locate any installed JDKs, I was unable to find JDK 17. Also, running /usr/libexec/java_home -V does not show JDK 17.

I expected Flutter to use the JDK version as specified in the $JAVA_HOME environment variable. Instead, it appears to be using its own bundled version, irrespective of my system settings.

Does anyone have an idea about how to rectify this? Any advice or guidance would be greatly appreciated. Thank you!

like image 883
Janis Wehen Avatar asked Sep 12 '25 18:09

Janis Wehen


2 Answers

If Flutter is not using the JDK from the JAVA_HOME environment variable, you can explicitly set the JDK path for Flutter by using the flutter config command. Follow these steps:

Open your command prompt or terminal. Run the following command to set the JDK path for Flutter:

flutter config --jdk-dir "C:\Program Files\Java\jdk-11"

This command tells Flutter to use the specified JDK directory instead of relying on the JAVA_HOME environment variable.

Make sure to replace "C:\Program Files\Java\jdk-11" with the actual path to your JDK installation if it's different.

like image 102
Kiran Avatar answered Sep 14 '25 08:09

Kiran


Run

flutter config --jdk-dir $JAVA_HOME

Then restart your editor to apply the configuration change

like image 26
ganiular Avatar answered Sep 14 '25 10:09

ganiular