14

Trying to setup jenkins but my builds fails with:

$ ant test
Error: JAVA_HOME is not defined correctly.
  We cannot execute /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
Build step 'Invoke Ant' marked build as failure

If I manually run "ant test" on the machine it works just fine and the JAVA_HOME is set to the exact same value. Any idea why it fails when jenkins try to run it ? Are there any more environment variabled involved ( I could not see any though ) ?

Zitrax
  • 19,036
  • 20
  • 88
  • 110
  • 1
    Are you sure that `JAVA_HOME` is set for the user running Jenkins? – devnull Apr 16 '13 at 07:52
  • @devnull Yes, as if I change it the "We cannot execute..." message update to the new value while still failing. – Zitrax Apr 16 '13 at 07:59
  • maybe you need JDK, not JRE? – user1516873 Apr 16 '13 at 08:06
  • @user1516873 Tried that too, but same problem. – Zitrax Apr 16 '13 at 08:11
  • 1
    possible duplicate of [Jenkins, specifying JAVA\_HOME](http://stackoverflow.com/questions/8185593/jenkins-specifying-java-home) – andyb Apr 16 '13 at 08:11
  • I think `JAVA_HOME` should be the `/usr/lib/jvm/java-7-openjdk-amd64` root directory. Ant seems to guess `JAVA_HOME` when it's incorrect or not set according to http://stackoverflow.com/a/1558792/637889 which might explain why Ant works and Jenkins does not. – andyb Apr 16 '13 at 08:17

4 Answers4

10

For others, I had to add the PATH /usr/bin/ to my PATH variable within Jenkins. (Find your correct path using which java).

Jenkins > Manage Jenkins > Configure System

Add an Environment Variable >>

e.g:

Name: PATH
Value: /usr/local/bin/:/usr/bin/

Screenshot

enter image description here

Anil
  • 21,730
  • 9
  • 73
  • 100
4

The problem was this, I had forgot to check the box "Restrict where this project can be run" in the project configuration. Thus the testing tried to execute on "master" where JAVA_HOME was not the same as expected on the build executor I intended to run it on. Thus where I tested and where it actually ran was different machines.

Zitrax
  • 19,036
  • 20
  • 88
  • 110
3

Set JAVA_HOME in your Jenkins system config in Jenkins 2.107.1.

open your jenkins, e.g. http://192.168.1.12:9090, then add /configure to the url, that is http://192.168.1.12:9090/configure, then you can find like next:

enter image description here

frank
  • 1,169
  • 18
  • 43
1

For me the options above did not help, solved by creating a link to what's asked:

sudo ln -s ${actual_java_location} /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java

actual_java_location can be read by this:

 readlink -f $(which java)
Bryn
  • 385
  • 3
  • 15