3

I'm trying to run the .net core sonarscanner tool on Jenkins (running on Linux) like:

dotnet sonarscanner begin ...

But it's giving the error

Could not find 'java' executable in JAVA_HOME or PATH.

I'm not sure why this is as I am explicitly setting the path at the top of my pipeline:

environment {
    JAVA_HOME="${tool 'openjdk-11'}/jdk-11"
    PATH="${tool 'openjdk-11'}/jdk-11/bin:$HOME/.dotnet/tools/:$PATH" 
}  

And from running the following commands I can see this looks correct:

sh "echo \"Java Home:\" $JAVA_HOME"

Produces:

Java Home: /var/lib/jenkins/tools/hudson.model.JDK/openjdk-11

And listing the directory:

sh "ls /var/lib/jenkins/tools/hudson.model.JDK/openjdk-11/jdk-11/bin"

produces:

+ ls /var/lib/jenkins/tools/hudson.model.JDK/openjdk-11/jdk-11/bin
jaotc
jar
jarsigner
java
javac
javadoc
javap
jcmd
jconsole
jdb
jdeprscan
jdeps
jhsdb
jimage
jinfo
jjs
jlink
jmap
jmod
jps
jrunscript
jshell
jstack
jstat
jstatd
keytool
pack200
rmic
rmid
rmiregistry
serialver
unpack200

Yet still, sonar scanner fails with the error:

Could not find 'java' executable in JAVA_HOME or PATH.

UPDATE:

Regarding Dimitry's comment - I am using the tools:

tools

With this at the top of the pipeline:

  tools{
      jdk 'openjdk-11'
  }

With regards to Marcinek's answer - good spot. I've realised that setting the JAVA_HOME at the top of the pipeline is not actually doing anything. Whatever I set it to, it remains as:

/var/lib/jenkins/tools/hudson.model.JDK/openjdk-11

David Masters
  • 8,069
  • 2
  • 44
  • 75
  • There should be a jenkins configuration where Java is supposed to be. See here: https://subscription.packtpub.com/book/networking_and_servers/9781788297943/1/ch01lvl1sec20/configuring-java-home-in-jenkins Because there can be multiple java installations. – Marcinek Nov 29 '20 at 14:34
  • I'm running into this same issue. Were you able to resolve it? – Jeromy French Nov 10 '21 at 20:02
  • 1
    @JeromyFrench you probably solved this ages ago, but for anyone else running into the same issue: Sonarqube is shipped with its own JRE, and execution of the java executable in there must be permitted using `chmod` first. See this answer: https://stackoverflow.com/a/62587660/808151 – Tim Meyer Aug 09 '22 at 15:50

2 Answers2

0

The variable $JAVA_HOME should point to the main directory of java, where the bin folder can be found by appending bin

Your $JAVA_HOME variable is pointing to

Java Home: /var/lib/jenkins/tools/hudson.model.JDK/openjdk-11

To find the java executable you have to append jdk-11/bin

Thus the correct path to JAVA_HOME should be:

/var/lib/jenkins/tools/hudson.model.JDK/openjdk-11/jdk-11

And the PATH should point to:

/var/lib/jenkins/tools/hudson.model.JDK/openjdk-11/jdk-11/bin
Marcinek
  • 2,144
  • 1
  • 19
  • 25
0

You can use 'tools' section to use java in your pipeline.

pipeline {
    agent any
    tools {
        jdk 'your-jdk-tool-name' 
    }
Dmitriy Tarasevich
  • 1,082
  • 5
  • 6