“Could not find ‘java’ executable in JAVA_HOME or PATH.” when scanning with dotnet tool

FROM mcr.microsoft.com/mssql/server:2019-latest

RUN wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb && \
    dpkg -i packages-microsoft-prod.deb && \
    apt-get update; \
    apt-get install -y apt-transport-https && \
    apt-get update && \
    apt-get install -y dotnet-sdk-5.0

CMD sleep infinity
dotnet tool install --global dotnet-sonarscanner
export PATH="$PATH:/root/.dotnet/tools"

dotnet sonarscanner begin /k:"test-key" /d:sonar.host.url="http://host.docker.internal:9000"  /d:sonar.login="token"

dotnet build /src/MyProject.csproj

dotnet sonarscanner end /d:sonar.login="token"

You can do any combination of the setup above with custom paths, with standalone SonarQube server or in Docker, with the same stuff locally or in a different Ubuntu based image (the one above is Ubuntu 20.04).

When it runs dotnet sonarscanner end it throws

SonarScanner for MSBuild 5.2.1
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.
The SonarScanner did not complete successfully

Pretty much the same as "Could not find ‘java’ executable in JAVA_HOME or PATH." when scanning with Scanner for NET but I cannot reply to that thread for some reason and there are no solutions or even clues provided to what’s happening.

The information listed here is wrong https://sonarcloud.io/documentation/analysis/scan/sonarscanner-for-msbuild/ - it requires corrections in order for the copy-paste to work.

I’d like to avoid installing java onto my images/machines. The way the doc above explains it it sounds like you do not need a standalone java setup. I also tried to apply chmod 755 on /root/.dotnet/tools/.store/dotnet-sonarscanner/5.2.1/dotnet-sonarscanner/5.2.1/tools/net5.0/any/sonar-scanner-4.6.1.2450/lib/sonar-scanner-cli-4.6.1.2450.jar and other jar files I could find. No luck.

Sounds like you still need JRE/JDK installed for the dotnet tool to work. This should definitely be mentioned in the docs. We do not have java installed anywhere as we do not need it. dotnet tool install sounds like a self sufficient installation, especially after seeing dotnet sonarscanner begin working. It’s weird that a part of the commands work straight out of the box and another part requires additional magic.

Fixed by running

apt-get install openjdk-8-jre

Hello @iSeiryu

Regarding the community thread that you linked, one of the answers is there that the Specific JDK needs to be installed separately.

And regarding the SonarScanner for .NET documentation you linked, the first bullet in the prerequisites is Java 11 or greater.

All the best,
Čaba

Touche.
And yet I see several questions about the same thing here and on StackOverflow.

Different answers and points of docs talk about stand-alone sonar-scanner executable. It gets confusing. It can lead to a misunderstanding that dotnet tool should have everything prepackaged for the executable to run. Which I wish it did. Or at least it had 2 separate packages:

dotnet tool install --global dotnet-sonarscanner
dotnet tool install --global dotnet-sonarscanner-complete

Also, “Specific JDK needs to be installed separately” and “Java 11 or greater”.
I installed JRE version 8 and it works without complaints. I think JRE should be offered as a solution first as not everyone needs the full JDK.

Thanks.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.