11

I have set my JAVA_HOME to /usr/lib/jvm/java-7-oracle, which contains a valid JVM, but gradle insists on using /usr/lib/jvm/default-java and complains about JAVA_HOME:

$ echo $JAVA_HOME 
/usr/lib/jvm/java-7-oracle
$ ls $JAVA_HOME
bin  COPYRIGHT  db  include  jre  lib  LICENSE  man  README.html  release  src.zip  THIRDPARTYLICENSEREADME-JAVAFX.txt  THIRDPARTYLICENSEREADME.txt
$ gradle assemble

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.

How can I fix this and have gradle use my valid JAVA_HOME?

Note: My JAVA_HOME is set by ~/.bashrc using export JAVA_HOME="/usr/lib/jvm/java-7-oracle"

Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373

2 Answers2

25

it is quite common issue. Just go to your gradle home directory and bin, for example:

/home/user/gradle-2.0/bin

and there you should have gradle script, I am on a Windows machine now so I have gradle.bat, probably you will have gradle.sh or something like this. In that file, find the line:

set JAVA_HOME=%JAVA_HOME:"=%

or as I can see you are on linux so there will be "export" and change it to your java home directory for example:

export JAVA_HOME=/usr/lib/jvm/java-7-oracle
Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
SuperAndrew
  • 3,013
  • 1
  • 17
  • 10
  • 6
    Indeed! Line 70 of `/usr/share/gradle/bin/gradle` is `export JAVA_HOME=/usr/lib/jvm/default-java`. Removing that line fixes the problem. – Nicolas Raoul Nov 12 '14 at 09:02
  • 1
    This happens in newer versions also. where the 70th line mentioned here is removed in gradle script. This is how I fixed it, run `which java` and take it's result. remove the `/bin/java` from that and use it to set the `JAVA_HOME`. Then call the `gradlew` command. – rahulrvp Sep 09 '16 at 11:18
1

If on windows, also make changes to JAVA_EXE and add your path

    set JAVA_EXE=C:\yourpath\bin\java.exe
Armaan Dhir
  • 128
  • 1
  • 5