Windows 10 Gradle: JAVA_HOME is set to an invalid directory

Viewed 24316

I was trying to run ./gradlew bootRun in both Git Bash and PowerShell, and got this error: ERROR: JAVA_HOME is set to an invalid directory: C:\Program Files\Java\jre1.8.0_121

The gradlew file is inside the project directory that cloned from git. It is a practice project from Spring official guide. There's also a gradle.bat file with these lines:

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%\bin\java.exe 

I also tried to run Eclipse EE, which got no problem. The environment setting should be no problem.

6 Answers

In addition to Ernest's answer, the JAVA_HOME it wants is the actual directory, not the bin folder like the path wants for javac. Took me a while to work that out

Please set the JAVA_HOME variable location to the path to JDK instead of JRE.

If that is correctly set, check the environment variables in Edit mode. In my case, I found that a semicolon was appended to the JAVA_HOME environment variable at the end.

To check:

  1. Open the environment variables list.
  2. Select the variable and click Edit.
  3. Remove the semicolon and Save.

Closed the settings and checked the path in cmd using echo %JAVA_HOME%, and ran gradle -v.

It ran perfectly.

Note: Check the variable value by clicking the Edit button as we cannot see the semicolon instead. Windows adds a semicolon so that the same variable can be used as the path to multiple locations.

Added picture for reference: Environment variable in Edit mode

I've found myself with the same issue, although my JAVA_HOME was set to the correct JDK path. However Windows 10 appended a semicolon at the end of the path. This way the JAVA_HOME variable would work anywhere but not with Gradle. I'm posting this, because it might spare someone else the time I've been looking for it ;)

Found a solution I hadn't seen before in my googling.

Open up your gradlew.bat in some editor. On line 34-ish you will see this:

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%\bin\java.exe

if exist "%JAVA_EXE%" goto init

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

add this line to the error message: echo %JAVA_EXE%

so it looks like this

echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo %JAVA_EXE%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

Then run your gradlew.bat file.

The error should now appear again, but with the added echo of the path to JAVA_EXE. In my case it was echoing \bin\java.exe and NOT the full path C:\...\Java\jre1.8.0_251\bin\java.exe

to fix this, I changed

set JAVA_EXE=%JAVA_HOME%\bin\java.exe

to

set JAVA_EXE=C:\...\Java\jdk1.8.0_261\bin\java.exe

The value is now hardcoded, true, but it worked. I don't know why gradle doesn't concat the variable name into the path.

If you didn't have admin rights, override the JAVA_HOME in User Environment Variable. Avoid typo by selecting the Java home folder. You must include folder before the bin ( do not include /bin) for java home.

Related