简体   繁体   English

Windows 10 Gradle:JAVA_HOME 设置为无效目录

[英]Windows 10 Gradle: JAVA_HOME is set to an invalid directory

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 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. gradlew文件位于从 git 克隆的项目目录中。 It is a practice project from Spring official guide .它是Spring 官方指南中的一个实践项目。 There's also a gradle.bat file with these lines:还有一个gradle.bat文件,其中包含以下几行:

: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.我还尝试运行Eclipse EE ,没有问题。 The environment setting should be no problem.环境设置应该没问题。

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.除了欧内斯特的回答,它想要的 JAVA_HOME 是实际目录,而不是像 javac 路径想要的 bin 文件夹。 Took me a while to work that out我花了一段时间才解决这个问题

Looks like your JAVA_HOME variable resolves to a JRE installation directory.看起来您的 JAVA_HOME 变量解析为 JRE 安装目录。 But as you're talking about Gradle, you're surely trying to build/compile code.但是当您谈论 Gradle 时,您肯定是在尝试构建/编译代码。

If you have a JDK installed, change JAVA_HOME variable to point to it.如果您安装了 JDK,请将 JAVA_HOME 变量更改为指向它。 The folder name should typically start with "jdk".文件夹名称通常应以“jdk”开头。 Otherwise, you have to install a Java Development Kit.否则,您必须安装 Java 开发工具包。

You could also try exporting the variable manually before running gradle on the command line ( SET JAVA_HOME=<path to the jdk directory> )您也可以尝试在命令行上运行 gradle 之前手动导出变量( SET JAVA_HOME=<path to the jdk directory>

Please set the JAVA_HOME variable location to the path to JDK instead of JRE.请将 JAVA_HOME 变量位置设置为 JDK 而不是 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.在我的例子中,我发现在 JAVA_HOME 环境变量末尾附加了一个分号。

To check:去检查:

  1. Open the environment variables list.打开环境变量列表。
  2. Select the variable and click Edit. Select 变量并单击编辑。
  3. Remove the semicolon and Save.删除分号并保存。

Closed the settings and checked the path in cmd using echo %JAVA_HOME% , and ran gradle -v .关闭设置并使用echo %JAVA_HOME%检查 cmd 中的路径,然后运行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. Windows 添加了分号,以便可以将同一个变量用作多个位置的路径。

Added picture for reference:添加图片供参考: 编辑模式下的环境变量

I've found myself with the same issue, although my JAVA_HOME was set to the correct JDK path.我发现自己遇到了同样的问题,尽管我的 JAVA_HOME 设置为正确的 JDK 路径。 However Windows 10 appended a semicolon at the end of the path.但是,Windows 10 在路径末尾附加了一个分号。 This way the JAVA_HOME variable would work anywhere but not with Gradle.这样 JAVA_HOME 变量可以在任何地方工作,但不适用于 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.在某个编辑器中打开您的 gradlew.bat。 On line 34-ish you will see this:在第 34-ish 行,您将看到:

: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%将此行添加到错误消息中: 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.然后运行你的 gradlew.bat 文件。

The error should now appear again, but with the added echo of the path to JAVA_EXE.该错误现在应该再次出现,但添加了 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就我而言,它正在回显\\bin\\java.exe而不是完整路径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.我不知道为什么 gradle 没有将变量名连接到路径中。

If you didn't have admin rights, override the JAVA_HOME in User Environment Variable.如果您没有管理员权限,请覆盖用户环境变量中的 JAVA_HOME。 Avoid typo by selecting the Java home folder.通过选择 Java 主文件夹来避免拼写错误。 You must include folder before the bin ( do not include /bin) for java home.您必须在 java 主页的 bin(不包括 /bin)之前包含文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM