Windows 10 Gradle: JAVA_HOME is set to an invalid directory
Asked Answered
E

9

14

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.

Equip answered 20/5, 2017 at 4:41 Comment(1)
Do you have Java installed in that folder?Potts
C
11

Looks like your JAVA_HOME variable resolves to a JRE installation directory. But as you're talking about Gradle, you're surely trying to build/compile code.

If you have a JDK installed, change JAVA_HOME variable to point to it. The folder name should typically start with "jdk". Otherwise, you have to install a Java Development Kit.

You could also try exporting the variable manually before running gradle on the command line (SET JAVA_HOME=<path to the jdk directory>)

Conroy answered 20/5, 2017 at 4:59 Comment(0)
S
26

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

Stepper answered 26/4, 2018 at 23:12 Comment(1)
THIS was what solved it for me. I didn't realize this.Urbanity
C
11

Looks like your JAVA_HOME variable resolves to a JRE installation directory. But as you're talking about Gradle, you're surely trying to build/compile code.

If you have a JDK installed, change JAVA_HOME variable to point to it. The folder name should typically start with "jdk". Otherwise, you have to install a Java Development Kit.

You could also try exporting the variable manually before running gradle on the command line (SET JAVA_HOME=<path to the jdk directory>)

Conroy answered 20/5, 2017 at 4:59 Comment(0)
I
9

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

Incursive answered 9/10, 2021 at 11:13 Comment(0)
S
7

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.

Selfheal answered 11/9, 2020 at 9:9 Comment(0)
J
4

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 ;)

Josejosee answered 31/8, 2020 at 14:48 Comment(0)
O
0

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.

Obtrude answered 8/9, 2022 at 0:16 Comment(0)
L
0

If none of the above works open the gradle.bat file in an editor and make sure that IT is correct , in my case gradle was the problem

Legg answered 2/11, 2022 at 9:46 Comment(1)
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewMiliary
K
0

The pictorial representation of @Kyrei answer is :

[JAVA_HOME variable data (C:\your\java\folder\loc\\)][1]

And I also removed semicolon(;) from the last part which is inserted by default by windows.

P.S: as my reputaion is very low that's why I can't put this image in the "comment" section.

Kakemono answered 2/6, 2023 at 16:51 Comment(0)
B
0

If you have installed Android Studio

It ships with Java already.

Windows 10/11 access the Select new environment variable and add the following value/path:

JAVA_HOME
C:\Program Files\Android\Android Studio\jbr\bin\java

Also I recommend downloading gradle from https://services.gradle.org/distributions/ ( https://services.gradle.org/distributions/gradle-8.3-all.zip to be specific or more recent ) and extract it to:

C:\Users\{your username}\.gradle\wrapper\dists

And then add it to your (system) path environment variable

Balsamiferous answered 6/9, 2023 at 15:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.