[英]Gradle can't locate Java when running “./gradlew shadowJar”
I'm trying to build a jar
file that includes the MySQL driver
for JDBC
. 我正在尝试构建一个包含用于
JDBC
的MySQL driver
的jar
文件。 So far I have a very basic build.gradle
file that is using the shadowJar
plugin to help build this jar
file. 到目前为止,我有一个非常基本的
build.gradle
文件,该文件正在使用shadowJar
插件来帮助构建此jar
文件。
The problem is, when running anything Gradle related, I receive this error: 问题是,当运行任何与Gradle相关的东西时,我收到此错误:
ERROR: JAVA_HOME is set to an invalid directory: /usr/lib/jvm/jdk-11.0.1/bin/java
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.
When trying to locate Java with $ which java
I get 当尝试使用
$ which java
获得Java时找到Java
/usr/bin/java
and with $ ls -la /usr/bin | grep -i javaj
和
$ ls -la /usr/bin | grep -i javaj
$ ls -la /usr/bin | grep -i javaj
I receive 我收到
$ ls -la /usr/bin | grep -i javaj
lrwxrwxrwx 1 root root 22 Sep 21 11:32 java -> /etc/alternatives/java
lrwxrwxrwx 1 root root 29 Nov 5 21:12 java2groovy -> /etc/alternatives/java2groovy
lrwxrwxrwx 1 root root 23 Nov 5 20:33 javac -> /etc/alternatives/javac
lrwxrwxrwx 1 root root 25 Nov 5 20:33 javadoc -> /etc/alternatives/javadoc
lrwxrwxrwx 1 root root 23 Nov 5 20:33 javah -> /etc/alternatives/javah
lrwxrwxrwx 1 root root 23 Nov 5 20:33 javap -> /etc/alternatives/javap
So far none of this information has been helpful for me but I'm leaving it here just in case. 到目前为止,这些信息都没有对我有所帮助,但是为了以防万一,我将其保留在此处。
Also, when I use $ readlink -f $(which java)
I receive: 另外,当我使用
$ readlink -f $(which java)
我收到:
/usr/lib/jvm/jdk-11.0.1/bin/java
Which to me seems like it is the appropriate path that Gradle is trying to look. 在我看来,这似乎是Gradle尝试寻找的适当路径。
Lastly, in my .bashrc
I have put this in the file to set my JAVA_HOME
: 最后,在我的
.bashrc
文件中,将其放入文件中以设置JAVA_HOME
:
## JAVA ##
export JAVA_HOME="/usr/lib/jvm/jdk-11.0.1/bin/java"
export PATH=$PATH:/usr/lib/jvm/jdk-11.0.1/bin
Any ideas on why running $ ./gradlew shadowJar
would not be able to find Java? 为什么运行
$ ./gradlew shadowJar
无法找到Java?
According to this question which seems similar to my question, my version of Gradle is trying to set its own JAVA_HOME
path which I've figured out it is being set to /usr/bin/java/bin/java
which does not exist. 根据这个与我的问题相似的问题,我的Gradle版本试图设置自己的
JAVA_HOME
路径,我发现它已被设置为/usr/bin/java/bin/java
,而该路径不存在。
On lines 70 - 89 there is an if/else
statement doing this 在第70-89行,有一个
if/else
语句在执行此操作
Instead of hiding the solution in comments, I'll create an answer. 我将创建一个答案,而不是在注释中隐藏解决方案。 This is marginally on-topic since it has to do not only with how
JAVA_HOME
is set on a Linux/POSIX system, but also how the Gradle Wrapper behaves different than a given Gradle executable. 这几乎是话题,因为它不仅与在Linux / POSIX系统上如何
JAVA_HOME
有关,而且还与Gradle Wrapper与给定的Gradle可执行文件的行为方式不同。
In this manner it is related to, but not quite the same as this Q&A which has a lot of nice hints in it. 通过这种方式,它与该问答相关但并不完全相同, 问答中有很多不错的提示 。
In a nutshell: 简而言之:
gradlew
uses JAVA_HOME
if set, otherwise will attempt to just run java
without any JAVA_HOME
. gradlew
如果设置了JAVA_HOME
则使用JAVA_HOME
,否则将尝试仅运行java
而不使用任何JAVA_HOME
。 It'll log to the console about this, and that it can't really continue. JAVA_HOME
is always expected to be set to a path that resolves to a directory , not a file . JAVA_HOME
设置为可解析为目录而非文件的路径。 In this case, JAVA_HOME
was set to the actual JDK Java executable, and there was some confusion about the Linux "alternatives" (how multiple packages and package versions can fulfill system requirements) and how those work with JAVA_HOME
. 在这种情况下,
JAVA_HOME
被设置为实际的JDK Java可执行文件,并且对于Linux“替代方案”(多个软件包和软件包版本如何满足系统要求)以及它们如何与JAVA_HOME
一起使用存在一些困惑。 (Most of that confusion was me misunderstanding the question!) (大多数困惑是我误解了这个问题!)
This meant that the Gradle Wrapper was (in this case) trying to invoke /usr/lib/jvm/jdk-11.0.1/bin/java/bin/java
(note the double ref to "bin/java") as the JAVA_CMD
which, of course, doesn't exist. 这意味着(在这种情况下)Gradle Wrapper试图将
/usr/lib/jvm/jdk-11.0.1/bin/java/bin/java
(请注意对“ bin / java”的双重引用)作为JAVA_CMD
当然不存在。
Once we set JAVA_HOME
to the expected JDK location the Gradle Wrapper ran successfully. 将
JAVA_HOME
为预期的JDK位置后,Gradle Wrapper成功运行。
AFAIK, the alternatives system does not have any conveniences for providing a JAVA_HOME
like reference to the underlying JDK location. AFAIK,替代系统在提供类似于
JAVA_HOME
类的对底层JDK位置的引用方面没有任何便利。 I suppose one could script the output from the alternatives
command to get the currently installed and active JDK location. 我想可以用脚本
alternatives
命令的输出来获取当前已安装且处于活动状态的JDK位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.