The error message “JAVA_HOME is not set and no ‘java’ command could be found in your flutter PATH” is a common issue faced by developers when trying to run Flutter applications on Windows systems. This error occurs due to the absence of the Java Development Kit (JDK) or incorrect configuration of the JAVA_HOME environment variable.
Solution 1: Set the JAVA_HOME Environment Variable
To resolve this issue, you need to set the JAVA_HOME environment variable on your Windows system. Here’s how to do it:
Download the Java JDK from here. (Note: Make sure to download the correct version of the JDK, as different versions may have different requirements.) Install the downloaded Java JDK on your system.
Step-by-Step Instructions for Setting JAVA_HOME Environment Variable:
Step 1: Open Windows Search
Windows Search > env > Edit the system environment variables
Step 2: Click on “Environment Variables…” and then click on “New”.
Step 3: Fill in the “variable name” field with “JAVA_HOME”.
Variable Name: JAVA_HOME
Step 4: Fill in the “variable value” with the path to where Java is installed on your computer (for me it was under “C:\Program Files\Java\
Variable Value: C:\Program Files\Java\jdk-15.0.2
Step 5: Click “OK” and close all dialogs.
Tips:
- Make sure to restart your IDE/Terminal after setting the JAVA_HOME environment variable.
- Ensure that you have downloaded the correct version of Java JDK for your system configuration.
Solution 2: Use flutter config –android-studio-dir Command (For Windows)
If you are using Flutter on a Windows system, an alternative solution to setting the JAVA_HOME environment variable is to use the command `flutter config –android-studio-dir` to configure Android Studio settings within your Flutter configuration.
Step-by-Step Instructions for Using flutter config Command:
flutter config --android-studio-dir="C:\Program Files\Android\Android Studio"
This command will instruct Flutter to use the specified directory of Android Studio, which contains the required Java settings and tools.
Additional Tips (Bonus)
For added convenience when working with Android Studio on Windows, it’s recommended to download the “Android SDK Command-line Tools” from within your Android Studio interface. This will provide you with necessary tools for handling Android licenses (image reference).
By following these steps and utilizing the `flutter config` command, you should be able to resolve the “JAVA_HOME is not set” error in your Flutter PATH on Windows systems.