简体   繁体   English

Java JRE / JDK和JAVA_HOME或java.home的问题

[英]Gradle problems with Java JRE/JDK and JAVA_HOME or java.home

Windows 10 OS. Windows 10操作系统。 I'm using a Cygwin BASH console to run Gradle. 我正在使用Cygwin BASH控制台运行Gradle。

I've been struggling with this for a few hours now. 我已经为此奋斗了几个小时。 There are one or two questions on SO which seem to relate this but I'm still a long way from understanding the basic mechanisms and how to get Gradle to use the right JDK/JRE. 关于SO,有一个或两个问题似乎与此相关,但是距离了解基本机制以及如何使Gradle使用正确的JDK / JRE仍有很长的路要走。

This started earlier when gradle build didn't work and complained that it couldn't find "tools.jar". 当gradle构建无法正常工作并抱怨找不到“ tools.jar”时,这开始于较早。 This appears to be a sign that the path to the Java JRE used to run the Gradle app isn't associated with a JDK. 这似乎表明用于运行Gradle应用程序的Java JRE的路径未与JDK关联。 This is indeed the case: the JRE it insists on using to start up is at "C:\\Program Files\\Java\\jre1.8.0_73\\". 确实是这样:它坚持使用的JRE位于“ C:\\ Program Files \\ Java \\ jre1.8.0_73 \\”。 This is a JRE without a JDK. 这是没有JDK的JRE。

The JDK I want Gradle to use is at "D:\\apps\\Java\\jdk1.8.0_191\\". 我想让Gradle使用的JDK位于“ D:\\ apps \\ Java \\ jdk1.8.0_191 \\”。

I have set JAVA_HOME to this value. 我已将JAVA_HOME为此值。 I have also placed "D:\\apps\\Java\\jdk1.8.0_191\\bin\\" at the very start of the PATH environment variable. 我还将PATH环境变量的开头放置了“ D:\\ apps \\ Java \\ jdk1.8.0_191 \\ bin \\”。

In both Cygwin and the W10 console, when I go "java -version" it prints out the 1.8.0_191 version, proving that the OS System is configured as expected. 在Cygwin和W10控制台中,当我转到“ java -version”时,它将打印出1.8.0_191版本,证明OS系统已按预期配置。

But Gradle itself insists on using the JRE in C:. 但是Gradle本身坚持在C:中使用JRE。
I tried numerous experiments and rebooted numerous times. 我尝试了无数次实验,然后重启了无数次。 When I went, in build.gradle: 当我去的时候,在build.gradle中:

println "java.home is ${System.properties['java.home']}"

... it always printed out the C:\\ location version (1.8.0_73) ...它总是打印出C:\\位置版本(1.8.0_73)

Finally I made this impossible by renaming "C:\\Program Files\\Java\\" to "C:\\Program Files\\JavaXXX\\". 最后,通过将“ C:\\ Program Files \\ Java \\”重命名为“ C:\\ Program Files \\ JavaXXX \\”,使此操作变得不可能。 This then gives: 然后给出:

Chris@M15B /cygdrive/d/My Documents/software projects/EclipseWorkspace/LuceneIndexer_3
$ gradle build Error: could not open `C:\Program Files\Java\jre1.8.0_73\lib\amd64\jvm.cfg'

So it appeared, as far as I could surmise, that something inside Gradle has got its heart stubbornly set on using a particular JRE location... which disregards the JAVA_HOME variable, the PATH entries and the OS's own understanding of which JDK/JRE should be used... and even sets System setting java.home to a "spurious" setting (or one I don't want to use). 因此,据我所知,似乎Gradle中的某些东西已经顽固地开始使用特定的JRE位置……而无视JAVA_HOME变量, PATH条目以及操作系统对应该使用哪种JDK / JRE的理解使用...甚至将系统设置java.home设置为“虚假”设置(或我不想使用的设置)。

Finally... I put this line in gradle.properties: 最后,我将这一行放在gradle.properties中:

org.gradle.java.home=D:\\apps\\Java\\jdk1.8.0_191

When I then changed "C:\\Program Files\\JavaXXX\\" back to "C:\\Program Files\\Java\\", finally the thing built properly. 然后,当我将“ C:\\ Program Files \\ JavaXXX \\”更改回“ C:\\ Program Files \\ Java \\”时,终于可以正常构建了。 And finally java.home was printed out as "D:\\apps\\Java\\jdk1.8.0_191". 最终java.home被打印为“ D:\\ apps \\ Java \\ jdk1.8.0_191”。 But when I again renamed to "C:\\Program Files\\JavaXXX\\" the "could not open" error returned. 但是,当我再次重命名为“ C:\\ Program Files \\ JavaXXX \\”时,返回“无法打开”错误。 In other words, even if you can configure java.home in Gradle, Gradle itself appears to be reliant on a version of Java that you don't want it to use, and which is not the version for which the OS is configured! 换句话说,即使您可以在Gradle中配置java.home ,Gradle本身也似乎依赖于您不希望使用的Java版本,而不是为其配置操作系统的版本!

How on earth does the Gradle application find the JRE it is going to use to do its business? Gradle应用程序究竟如何找到将用于开展业务的JRE? Surely there must be a way to get it to use another one? 肯定有办法让它使用另一个吗?

later 后来

I also tried adding some ECHO statements to the gradle startup script... in which there is a specific section devoted to determining the JVM to use. 我还尝试将一些ECHO语句添加到gradle启动脚本中...在其中,有一个特定的部分专门用于确定要使用的JVM。 These experiments showed clearly that this script is correctly using the desired JDK (under D:). 这些实验清楚地表明该脚本正确地使用了所需的JDK(在D:下)。 Nor can it be a Cygwin-related problem, as other Cygwin commands work fine when Java\\ is renamed to Java_XXX. 它也不是与Cygwin有关的问题,因为当Java \\重命名为Java_XXX时,其他Cygwin命令也可以正常工作。 I also examined the registry: no clues found. 我还检查了注册表:未找到任何线索。 By going "gradle" at the Cygwin command prompt, something somewhere in the works insists on using a baked-in JRE location, even before the gradle startup script is run... 通过在Cygwin命令提示符下进行“渐变”,即使在运行gradle启动脚本之前,工作中某些地方仍坚持使用gradle JRE位置...

I was able to resolve this by upgrading my system's Java and Gradle. 我可以通过升级系统的Java和Gradle来解决此问题。 I changed to Java 9 (although apparently we're on Java 11 at the time of writing). 我更改为Java 9(尽管显然在撰写本文时我们使用的是Java 11)。 I also changed to Gradle 5.2.1 (from 3.5). 我还更改了Gradle 5.2.1(从3.5)。 I think I concluded that Gradle 3.5 was incompatible with Java 9... 我想我得出的结论是Gradle 3.5与Java 9不兼容...

I was quite confused: for a moment it appeared that Cygwin was out of date and that I needed a newer version of its Gradle. 我很困惑:一会儿就显得Cygwin的是过时的,我需要它的摇篮的新版本。 But in fact Cygwin was inheriting PATH from W10, and one of those PATH entries was "%GRADLE_HOME%\\bin". 但是实际上Cygwin从W10继承了PATH ,并且这些PATH条目之一是“%GRADLE_HOME%\\ bin”。 Cygwin was using the W10 Gradle. Cygwin正在使用W10 Gradle。 What fun to be using a Windoze OS! 使用Windoze操作系统有什么乐趣!

GRADLE_HOME (different from GRADLE_USER_HOME , where dependencies are downloaded!) thus had to be changed to the location of the new version of Gradle. GRADLE_HOME (来自不同GRADLE_USER_HOME ,其中依赖性下载!),因此,就必须改变到摇篮的新版本的位置。

All sorts of funny consequences followed for Eclipse: the dreaded red exclamation marks... caused by the fact that under "Project and External Dependencies" for several projects the dependencies revealed themselves to be located in the old 3.5 GRADLE_USER_HOME location, which I had renamed deliberately to make it inaccessible. Eclipse产生了各种有趣的后果:可怕的红色感叹号...是由于以下事实导致的:几个项目的“项目和外部依赖项”下,依赖项显示自己位于旧的3.5 GRADLE_USER_HOME位置,我已将其重命名为故意使其无法访问。 I found no way to "rebuild" these dependencies in Eclipse (ie to force use of GRADLE_USER_HOME , now pointing to a location under ...5.2.1), and instead opted to recreate the Eclipse projects. 我发现没有办法在Eclipse中“重建”这些依赖项(即,强制使用GRADLE_USER_HOME ,现在指向... 5.2.1下的位置),而是选择了重新创建Eclipse项目。

Quite trying. 非常尝试。 There must of course be a better way. 当然必须有更好的方法。

Incidentally, it does indeed appear that when you run a Gradle command in Cygwin the JVM as configured for your OS in the "Java Control Panel" gets invoked before the gradle startup script is run (... I think). 顺便说一句,确实出现了,当您在Cygwin中运行Gradle命令时,在运行gradle启动脚本之前会调用在“ Java控制面板”中为OS配置的JVM(...我认为)。 Even if your JAVA_HOME setting and first PATH entry are pointing to a different JRE (which they probably shouldn't be). 即使您的JAVA_HOME设置和第一个PATH条目指向的是另一个JRE(它们可能不应该)。

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

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