dotnet sonarscanner did not find java but JAVA_HOME exists. Tried jre 11 and 17 on ubuntu 20.04.What I am doing wrong?

Al 20 Reputation points
2024-01-16T19:41:11.96+00:00

~/project$ /usr/bin/dotnet sonarscanner end /d:sonar.login="admin" /d:sonar.password="user"SonarScanner for MSBuild 5.2.2 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 20:05:32.56 Post-processing failed. Exit code: 1

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,280 questions
{count} votes

Accepted answer
  1. Azizkhon Ishankhonov 540 Reputation points
    2024-01-16T21:16:18.1233333+00:00

    The error message "dotnet sonarscanner did not find java but JAVA_HOME exists" indicates that the SonarScanner for MSBuild is unable to locate the Java executable even though the JAVA_HOME environment variable is set correctly. This can be caused by a few different factors, including:

    • The Java executable is not in the PATH environment variable. The PATH variable is a list of directories that the system searches for executables when you type a command in the shell. Make sure that the bin directory of your Java installation is in the PATH variable.
    • The Java executable is not a valid executable. Verify that the java executable is indeed a Java executable by checking its file permissions and file type.
    • The Java installation is corrupted. Try reinstalling the Java installation to see if that resolves the issue.

    Here are some steps you can try to resolve the issue:

    1. Check the PATH variable. Open a terminal window and type the following command:
    echo $PATH
    

    This will print the contents of the PATH variable to the console. Make sure that the bin directory of your Java installation is in the list of directories. If it is not, add it to the end of the PATH variable using the following command:

    export PATH="$PATH:/path/to/java/bin"
    
    1. Verify the Java executable. Open a terminal window and type the following command:
    file /path/to/java/bin/java
    

    This will print the file type of the java executable. Make sure that the file type is "executable" and that the executable bit is set. If the file type is not "executable", try changing the file permissions using the following command:

    chmod +x /path/to/java/bin/java
    
    1. Reinstall Java. Uninstall the current Java installation and then install a new one. Make sure to download the correct version of Java for your operating system.
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Pinaki Ghatak 5,570 Reputation points Microsoft Employee
    2024-01-17T11:38:48.8733333+00:00

    Hello Al

    This could be due to a misconfiguration of the JAVA_HOME environment variable itself.

    Here are some steps you can take to resolve this issue:

    1. Check the JAVA_HOME path: Make sure that the JAVA_HOME environment variable points to the correct location of your Java installation. It should point to the root directory of the Java installation, not the bin directory. For example, if your Java installation is in /usr/lib/jvm/jre-openjdk, you should set JAVA_HOME as follows:
    export JAVA_HOME=/usr/lib/jvm/jre-openjdk
    
    1. Update the PATH: Add the bin directory of the Java installation to the PATH environment variable. Here’s how you can do it:
    export PATH="$JAVA_HOME/bin:$PATH"
    
    1. Verify the setup: You can verify if JAVA_HOME and PATH are set correctly by running echo $JAVA_HOME and java -version in the terminal. The former should print the path to your Java installation, and the latter should print the Java version.

    If you’ve followed these steps and are still encountering the issue, it might be worth checking if there are any known issues with the version of SonarScanner you’re using. Remember to replace /usr/lib/jvm/jre-openjdk with the actual path to your Java installation. If you’re unsure about the location of your Java installation, you can usually find it in /usr/lib/jvm/. I hope this helps, and if so, please tag this information as answered. This helps further community readers who may have similar problems.

    1 person found this answer helpful.
    0 comments No comments

  2. Al 20 Reputation points
    2024-01-16T22:24:03.4166667+00:00

    There are SonarQube 8.9.10 in docker and .net 5 global tool sonarscanner 5.2.2.

    JAVA_HOME and PATH was set to the same java elf dynamic shared object with chmod +x. What's wrong? Thank you.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.