Gradle finds wrong JAVA_HOME even though it's correctly set
Solution 1
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.
Solution 2
add a symbolic link
sudo ln -s /usr/lib/jvm/java-7-oracle /usr/lib/jvm/default-java
Solution 3
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) )))
Solution 4
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
Solution 5
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
Related videos on Youtube
James Barnett
Day job as a web developer, but I'm also interested electronics, embedded systems, information security and image processing\computer vision. http://james-barnett.net
Updated on July 08, 2022Comments
-
James Barnett almost 2 years
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)
-
Peter Niederwieser about 10 yearsPlease 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? -
James Barnett about 10 yearsI'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) -
Peter Niederwieser about 10 yearsThe Gradle start script doesn't need
JAVA_HOME
to be set. IfJAVA_HOME
is set, the script uses$JAVA_HOME/bin/java
to run Gradle. Otherwise, it usesjava
(i.e.java
has to be on thePATH
). Perhaps the (third-party) apt package uses a modified start script. -
James Barnett about 10 yearsYea 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 -
IgorGanapolsky about 9 yearsDid you do
source ~/.bashrc
, or reboot your machine after setting JAVA_HOME? -
Manoj almost 7 yearsJust do export JAVA_HOME=/path/to/correct/java/version
-
-
James Barnett about 10 yearsYes, its defined in .bashrc as
export JAVA_HOME=/usr/lib/jvm/java-7-oracle
(slightly different syntax but I assume its the same) -
Lucas about 10 years@JamesBarnett, could the
gradle
command itself be setting that variable? Perhaps the person who installedgradle
created a wrapper script that set environment variables before starting the application. When using tomcat, it is not uncommon for thecatalina.sh
(which is the startup script) to call out to asetenv.sh
to set up environment variables which may includeJAVA_HOME
-
Ates Goral about 10 yearsThe 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.
-
Peter Niederwieser about 10 yearsGradle 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.
-
James Barnett about 10 yearsI updated the answer to make it clearer that the Ubuntu repo version is the problem, not Gradle itself.
-
Shashi almost 10 years@Nar could you please provide the installation path of JAVA
-
Nar almost 10 yearsThank you for answer but path of JAVA does not matter because gradle script overrides path as said James stackoverflow.com/a/22309017/1679348
-
Kay Schneider over 9 yearsThank 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
-
Phil over 9 yearsWorked just fine for me. Given the name (default-java), I'd say this is a fairly sensible approach.
-
sotrh about 9 yearsWay 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.
-
zorglub76 almost 9 yearsIn my opinion this is much better solution than changing Gradle files. (And yes, it works for me just fine)
-
Jaymes Bearden over 8 yearsAwesome. Problem is still around with Ubuntu 15.04
-
Ian McLaird about 8 yearsThis answer would be improved if you explained what this command does and why it's needed.
-
Basel Shishani over 7 yearsWe also now have sdkman (sdkman.io) to manage download/versions of gradle and other JVM sdk's.
-
rahulrvp over 7 yearsthe newer version of android studio also has the same issue. but in a different way. it seems like they have resolved the
export
issue. but we have to callsudo ./gradlew install
to get it work sometimes. -
Manoj almost 7 yearsI faced same issue. But in my GRADLE_HOME/bin/gradle file this line was not present -
export JAVA_HOME=/usr/lib/jvm/default-java
. Hence I couldn't figure out where does it pick the wrong one from. I am using gradle version 2.13 on RHEL 7.3. So to resolve this issue, I added the lineexport JAVA_HOME=/correct/path/to/java
in GRADLE_HOME/bin/gradle file. -
Inessa Pokromkina over 5 yearsalso i put this to my
~/.bashrc
to not enter this command everytime after reloadexport JAVA_HOME=/usr/lib/jvm/default-java
-
Andy Lorenz over 4 yearsits 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.
-
Borjante over 4 yearsKey here is to exclude /bin/java
-
Scott Stensland over 3 yearsthis is very fragile and will break once java gets upgraded to next release
-
keocra about 2 yearsI 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'.