1

When trying to build an android application from Jenkins using gradle, I get the following error:

ERROR: JAVA_HOME is set to an invalid directory: /usr/lib/jvm/java-8-oracle
Please set the JAVA_HOME variable in your environment to match the location of your Java installation.

Build step 'Invoke Gradle script' changed build result to FAILURE
Build step 'Invoke Gradle script' marked build as failure
Finished: FAILURE

However, when I check the JAVA_HOME variable I get:

Macs-Mac-mini:~ mac$ echo $JAVA_HOME 
/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home

Running java -version.

Macs-Mac-mini:~ mac$ java -version 
java version "1.8.0_112"
Java(TM) SE Runtime Environment (build 1.8.0_112-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.112-b16, mixed mode)

why does Gradle find /usr/lib/jvm/java-8-oracle

tried Gradle finds wrong JAVA_HOME and Gradle does not recognize the JAVA_HOME

Community
  • 1
  • 1
sam
  • 431
  • 1
  • 4
  • 16
  • "Running java -version also confirms that JAVA_HOME is set correctly and is on the PATH". That statement is incorrect, `java -version` uses the PATH and does not use `$JAVA_HOME` – lance-java Nov 29 '16 at 10:13
  • ok. corrected statement. – sam Nov 29 '16 at 11:07

2 Answers2

3

After lot of debugging i found that, Gradle was taking $JAVA_HOME variable value from Jenkins master JDK configuration not from node's $JAVA_HOME variable.

I removed default JDK configuration path from Jenkins master and Gradle took $JAVA_HOME variable value from node.

sam
  • 431
  • 1
  • 4
  • 16
0

You're not logged as a Jenkins user, so echo $JAVA_HOME only display the JAVA_HOME of the current user, which may not the same as Jenkins user.

Fortunately, you can set an environnement variable from the Jenkins user interface. (see this example, just replace JMETER_HOME with JAVA_HOME)

Community
  • 1
  • 1
dwursteisen
  • 11,435
  • 2
  • 39
  • 36