Could not find 'java' executable in JAVA_HOME or PATH. - Azure DevOps on Private Ubuntu Build Agent

  • ALM used - Azure DevOps
  • CI system used Azure DevOps
  • Languages of the repository - C#

I have a .net project which is built using a private ubuntu build agent. I am using a YAML build pipeline but with the stage

          - task: SonarCloudAnalyze@1
            displayName: Run Sonar Cloud Code Analysis
            continueOnError: false

Im getting the error

msbuild/SonarScanner.MSBuild.dll end
SonarScanner for MSBuild 5.13
Using the .NET Core version of the Scanner for MSBuild
Post-processing started.
Calling the SonarScanner CLI...
Could not find 'java' executable in JAVA_HOME or PATH.
##[error]The SonarScanner did not complete successfully
The SonarScanner did not complete successfully
##[error]16:50:28.171  Post-processing failed. Exit code: 1
16:50:28.171  Post-processing failed. Exit code: 1
##[error]The process '/home/devops/myagent/_work/_tool/dotnet/dotnet' failed with exit code 1
Finishing: Run Sonar Cloud Code Analysis

I can confirm java is installed on the agent, the version is

java --version
openjdk 17.0.8.1 2023-08-24
OpenJDK Runtime Environment (build 17.0.8.1+1-Ubuntu-0ubuntu120.04)
OpenJDK 64-Bit Server VM (build 17.0.8.1+1-Ubuntu-0ubuntu120.04, mixed mode, sharing)

And the agent has the following capabilities

Java - /usr/bin/java
JAVA_HOME - /usr/lib/jvm/java-17-openjdk-amd64/bin/java
JDK - /usr/bin/javac

Im struggling to understand what is wrong – I can confirm that my build works when I use the azure pipelines agent so its definitely an issue with the private build agent.

Here is my yaml

          - task: SonarCloudPrepare@1
            inputs:
              SonarCloud: SonarCloud
              organization: $(SonarCloud_Organization)
              scannerMode: MSBuild
              projectKey: $(SonarCloud_ProjectKey)
              projectName: $(SonarCloud_ProjectName)

          - task: DotNetCoreCLI@2
            displayName: .NET Build
            inputs:
              command: 'build'
              arguments: '--configuration $(buildConfiguration)'
              projects: '**/*.csproj'

          - task: SonarCloudAnalyze@1
            displayName: Run Sonar Cloud Code Analysis
            continueOnError: false

Any advice on how I can resolve this?

Hey there!

Can you tell me which specific version of the SonarCloud Extension for Azure DevOps you’re using?

Hi Colin,

The details are below - its the latest version

Extension details

Detect bugs, vulnerabilities and code smells across project branches and pull requests.

Publisher - SonarSource

Installed version - 1.42.0 (Latest)

Last updated - 9 Aug 2023 at 06:31 BST

Permissions - Build (read), Code (read), Test management (read)

Thanks!

For what it’s worth, here’s the code that resolves the location of Java.

It’s my understanding that agent capabilities ≠ environment variables, and are used to satisfy demands that route certain builds to certain agents. And, JAVA_HOME shouldn’t point directly to an executable but to the folder containing the executable.

  • Let’s try this: can you execute echo %JAVA_HOME% from within your pipeline (as a part of build) and come back with the output?
  • Out of curiosity, what version of the Azure DevOps agent are you using? I’m surprised this isn’t working out of the box.