ChatGPT解决这个技术问题 Extra ChatGPT

Gradle finds wrong JAVA_HOME even though it's correctly set

When trying to run gradle, I get the following error:

# gradle

ERROR: JAVA_HOME is set to an invalid directory: /usr/lib/jvm/default-java

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.

However, when I check the JAVA_HOME variable I get:

# echo $JAVA_HOME 
/usr/lib/jvm/java-7-oracle

My JAVA_HOME is defined in .bashrc and I have double checked that it is set as the source.

Running java -version also confirms that JAVA_HOME is set correctly and is on the PATH.

# java -version
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)

I have also checked that /usr/bin/java symlinks to /etc/alternatives/java which in turn correctly symlinks to /usr/lib/jvm/java-7-oracle/jre/bin/java

Additionally I've checked that there are no duplicate JAVA_HOME definitions in .bash_profile or /etc/profile.

So my question is how/why does Gradle find /usr/lib/jvm/default-java, and more importantly how do I point it to the correct directory?

Other programs which require the JDK work fine, so I think its a Gradle issue. I've also tried reinstalling Gradle which made no difference.

I'm running 64bit Xubuntu (Ubuntu 13.10 base)

Please provide the full error output. As it stands, it's not clear where the message comes from. Also, do you have a /usr/lib/jvm/default-java directory or symlink? And how did you install Gradle?
I've updated the Q, but that pretty much is the full error output. There is no /usr/lib/jvm/default-java dir. Installed with apt (apt-get install gradle)
The Gradle start script doesn't need JAVA_HOME to be set. If JAVA_HOME is set, the script uses $JAVA_HOME/bin/java to run Gradle. Otherwise, it uses java (i.e. java has to be on the PATH). Perhaps the (third-party) apt package uses a modified start script.
Yea looks like whatever ppa I got the binary from had hard coded and exported the JAVA_HOME to usr/lib/jvm/defult-java. Thanks for the help
Did you do source ~/.bashrc, or reboot your machine after setting JAVA_HOME?

C
Community

Turns out that the particular Gradle binary I downloaded from the Ubuntu 13.10 repository itself tries to export JAVA_HOME. Thanks to Lucas for suggesting this.

/usr/bin/gradle line 70:

export JAVA_HOME=/usr/lib/jvm/default-java

Commenting this line out solves the problem, and Gradle finds the correct path to the Java binary.

If you just download the binary from their website it does not have this problem, It's an issue with the Ubuntu repo version. There also seem to be some other issues with 13.10 version.


The gradle package appears to install the jdk to this location, at least on Ubuntu 12.04 LTS with only the default repositories. I did this on an Ubuntu VM that did not have Java, snapshotted it first, apt-get install gradle (which installs almost 400 packages), and it installed java to /usr/lib/jvm... reverted snapshot, no /usr/lib/jvm. I was testing the theory of gradle script exporting JAVA_HOME and indeed it does, as you found.
Gradle doesn't do this. The ppa is provided by someone else, and so you'd have to ask them. In general, I would recommend to bootstrap Gradle via the Gradle Wrapper, rather than installing it via a package manager.
I updated the answer to make it clearer that the Ubuntu repo version is the problem, not Gradle itself.
Thank you, i search for over an hour for an solution to this problem. Thanks for this post, it solves my problem on ubuntu 14.04
Way to solve your own problem (and mine simultaneously)! It's a shame that this question is a year old and the Gradle script in the repository hasn't been updated to fix this.
S
Shashi

add a symbolic link

sudo ln -s /usr/lib/jvm/java-7-oracle /usr/lib/jvm/default-java

@Nar could you please provide the installation path of JAVA
Thank you for answer but path of JAVA does not matter because gradle script overrides path as said James stackoverflow.com/a/22309017/1679348
Worked just fine for me. Given the name (default-java), I'd say this is a fairly sensible approach.
In my opinion this is much better solution than changing Gradle files. (And yes, it works for me just fine)
this is very fragile and will break once java gets upgraded to next release
S
Scott Stensland

Solution is to make JAVA_HOME == dir above bin where javac lives as in

type javac

javac is /usr/bin/javac   # now check if its just a symlink

ls -la /usr/bin/javac 

/usr/bin/javac -> /etc/alternatives/javac   # its a symlink so check again

ls -la /etc/alternatives/javac  # now check if its just a symlink

/etc/alternatives/javac -> /usr/lib/jvm/java-8-openjdk-amd64/bin/javac

OK so finally found the bin above actual javac so do this

export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH

above can be simplified and generalized to

which javac >/dev/null 2>&1 || die "ERROR: no 'javac' command could be found in your PATH"
export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which javac)  )))

J
Jyoti Prakash

For me this error was due to the reason Gradle as installed as sudo and I was trying as default user to run Gradle.

Try:

sudo gradle -version

or

sudo gradle -v

M
MJ Montes

In my Ubuntu, I have a headache for 2 days on this issue.

Step 1. Type on the terminal whereis java then it will display something like this

java: /usr/bin/java /etc/java /usr/share/java /usr/lib/jvm/java-8-openjdk-amd64/bin/java /usr/share/man/man1/java.1.gz

Step 2. Take note of the path: /usr/lib/jvm/java-8-openjdk-amd64/bin/java

exclude the bin/java

your JAVA_HOME = /usr/lib/jvm/java-8-openjdk-amd64


Key here is to exclude /bin/java
L
Lucas

Did you export your JAVA_HOME? Without export, the setting will not be propagated to the commands started inside of that shell. Also, java -version does not use JAVA_HOME, rather it uses the first java found in your path. Make sure your .bashrc looks something like this:

JAVA_HOME=/path/to/java/home
export JAVA_HOME

Yes, its defined in .bashrc as export JAVA_HOME=/usr/lib/jvm/java-7-oracle (slightly different syntax but I assume its the same)
@JamesBarnett, could the gradle command itself be setting that variable? Perhaps the person who installed gradle created a wrapper script that set environment variables before starting the application. When using tomcat, it is not uncommon for the catalina.sh (which is the startup script) to call out to a setenv.sh to set up environment variables which may include JAVA_HOME
M
Mohammed Safeer

Try installing latest version of gradle,

sudo add-apt-repository ppa:cwchien/gradle

sudo apt-get update

sudo apt-get install gradle

If we install from ubuntu repo, it will install the old version , (for me it was gradle 1.4). In older version, it sets java home from gradle as export JAVA_HOME=/usr/lib/jvm/default-java. Latest version don't have this issue.


W
Wasim

I faced this issue when I run the following command on Ubuntu:

ionic build android

To solve this issue, I did the following steps:

ln -sf /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java /usr/lib/jvm/default-java

Add JAVA_HOME to /etc/environment:

vi /etc/environment

Add:

JAVA_HOME="/usr/lib/jvm/default-java"

After saving, read it:

source /etc/environment

Finally, you can run build command.


n
nothingSpecial

I had the same problem, but I didnt find export command in line 70 in gradle file for the latest version 2.13, but I understand a silly mistake there, that is following,

If you don't find line 70 with export command in gradle file in your gradle folder/bin/ , then check your ~/.bashrc, if you find export JAVA_HOME==/usr/lib/jvm/java-7-openjdk-amd64/bin/java, then remove /bin/java from this line, like JAVA_HOME==/usr/lib/jvm/java-7-openjdk-amd64, and it in path>>> instead of this export PATH=$PATH:$HOME/bin:JAVA_HOME/, it will be export PATH=$PATH:$HOME/bin:JAVA_HOME/bin/java. Then run source ~/.bashrc.

The reason is, if you check your gradle file, you will find in line 70 (if there's no export command) or in line 75,

JAVACMD="$JAVA_HOME/bin/java" fi if [ ! -x "$JAVACMD" ] ; then die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME That means /bin/java is already there, so it needs to be substracted from JAVA_HOME path.

That happened in my case.


d
derloopkat

I have tested this on Manjaro Linux. Should work on other Disto too.

You need to include whole java-jdk dir instead of just java/bin for java env var.

For example, instead of:

export JAVA_HOME=/opt/jdk-14.0.2/bin #change path according to your jdk location PATH=$PATH:$JAVA_HOME

use this:

export JAVA_HOME=/opt/jdk-14.0.2/ #change path according to your jdk location PATH=$PATH:$JAVA_HOME

then run the gradle command it will work.


a
asp_NewBee

You can also go to the bin folder inside your gradle installation folder and correct the JAVA_HOME parameter in gradle.bat file. In my case, my JAVA_HOME was set to c:\Program files\java\bin The JAVA_HOME in gradle.bat was set to %JAVA_HOME%\bin\java.exe.

I corrected the JAVA_HOME in gradle.bat and it worked.

Thank you!!!


its now 2019 and this is STILL an issue! Except its slightly different and the variable at fault is now "set JAVA_EXE=%JAVA_HOME%/bin/java.exe" - remove the bin part from here to fix.
a
adiga

Before running the command try entering:

export JAVA_HOME="path_to_java_home"

Where path_to_java_home is the folder where your bin/java is.

If java is properly installed you can find it's location, by using the command:

readlink -f $(which java)

Don't forget to remove bin/java from the end of the path while putting it into JAVA_HOME


k
kickyblue

For me an explicit set on the arguments section of the external tools configuration in Eclipse was the problem.

https://i.stack.imgur.com/0UByl.png


V
Venkat Kotra
sudo ln -s /usr/lib/jvm/java-7-oracle/jre /usr/lib/jvm/default-java

Create a symbolic link to the default-java directory.

You can find your java directory by

readlink -f $(which java) 
# outputs: /usr/lib/jvm/java-7-oracle/jre/bin/java
# Remove the last `/bin/java` and use it in above symbolic link command.

This answer would be improved if you explained what this command does and why it's needed.
B
Bedrockbreaker

I had a problem with this too. It said wrong directory when it was correct. So I just created a local variable with the name of JAVA_HOME omitting the final /bin/java. It worked fine for me.


H
Harsh

If your GRADLE_HOME and JAVA_HOME environment are set properly then check your JDK directory and make sure you have java.exe file under below path.

C:\Program Files (x86)\Java\jdk1.8.0_181\bin

As error mentioned in gradle.bat file

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

It is not able to locate your java installation. So find and set

java.exe

under %JAVA_HOME%/bin if everything is correct.

This works for me (my account got disabled by client and their admin has removed java.exe from my directory.)


I was able to solve the described issue by deleting the gradle wrapper from my project directory and re-initializing it with a newer system gradle (7.4.2). The issue was the 'goto init'. There are two lines which changed to 'goto execute'.
O
O-9

[Windows] As already said, it looks like .bat -file tries to find java.exe from %JAVA_HOME%/bin/java.exe so it doesn't find it since bin is repeated twice in path. Remov that extra /bin from gradle.bat.

https://i.stack.imgur.com/KS5R8.jpg


l
lem-fr

In my dockercontainer (being minimal the problem of not finding java) was, that "which" was not installed. Comipling a project using gradlew used which in ./gradlew to find java Installing which solved the problem.


S
Sameera De Silva

Adding below lines in build.gradle solved my issue .

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now