'Supplied javaHome must be a valid directory. You supplied: C:\Program Files\Java\jdk1.8.0_241

I got this error message when I build ionic cordova for android:

Supplied javaHome must be a valid directory. You supplied: C:\Program Files\Java\jdk1.8.0_241

The problem is that path is different from what I have JAVA_HOME variable:

enter image description here

Any suggestion?



Solution 1:[1]

Open the CMD Command Prompt using Administrator privilege and check the value of the JAVA_HOME variable:

echo %JAVA_HOME%

The result should be the path to the JDK installation:

C:\Program Files\Java\jdk1.8.0_202

So, if the java version is a different version, you can set the correct one:

setx -m JAVA_HOME "C:\Program Files\Java\jdk1.8.0_241"
setx -m PATH "%PATH%;%JAVA_HOME%\bin";

Just do adjusts on the Java version as you need to correct the differences. Be sure you have the installed version that you are trying to set up.

The another solution is to clean the Gradle build cache.

gradlew cleanBuildCache

Solution 2:[2]

Step 1: download and install new jdk on different location and update the JAVA_HOME path.

Step 2: make sure to add bin folder path under path Environment variable, refer below picture.

add marked snippet under path (which is under system variable)

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
Solution 2 Pirate encrypted