简体   繁体   中英

Gradle can't locate Java when running “./gradlew shadowJar”

I'm trying to build a jar file that includes the MySQL driver for JDBC . So far I have a very basic build.gradle file that is using the shadowJar plugin to help build this jar file.

The problem is, when running anything Gradle related, I receive this error:

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

/usr/bin/java

and with $ ls -la /usr/bin | grep -i javaj $ ls -la /usr/bin | grep -i javaj I receive

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:

/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.

Lastly, in my .bashrc I have put this in the file to set my 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?

Edit

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.

On lines 70 - 89 there is an if/else statement doing this 在此处输入图片说明

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.

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:

  • The Gradle Wrapper gradlew uses JAVA_HOME if set, otherwise will attempt to just run java without any 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 .

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 . (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.

Once we set JAVA_HOME to the expected JDK location the Gradle Wrapper ran successfully.

AFAIK, the alternatives system does not have any conveniences for providing a JAVA_HOME like reference to the underlying JDK location. I suppose one could script the output from the alternatives command to get the currently installed and active JDK location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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