23

We are using SonarQube 5.1.2 using Ant runner 2.2 and Java pluging 3.12 for the analysis. I can succesfully analyse my project. I just keep getting this error:

Java bytecode has not been made available to the analyzer. The org.sonar.java.bytecode.visitor.DependenciesVisitor@d678716, org.sonar.java.checks.unused.UnusedPrivateMethodCheck@58e28efd, CycleBetweenPackages rule are disabled.

So I need to configure my sonar.java.binaries and sonar.java.test.binaries properties (following http://docs.sonarqube.org/display/PLUG/Java+Plugin+and+Bytecode).

Which I think I have done correctly:

<property name="project.dir" value="${basedir}/xalg.prj/h3_service_fo" />   
<property name="sonar.java.binaries" location="${project.build.dir}/classes/main" />
<property name="sonar.java.test.binaries" value="${project.build.dir}/classes/test" />

Which resolve to the following valid directories for the above properties:

basedir=D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj
project.dir=D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj/xalg.prj/h3_service_fo
sonar.java.binaries=D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj\\xalg.prj\\h3_service_fo\\build\\classes\\main
sonar.java.test.binaries=D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj/xalg.prj/h3_service_fo/build/classes/test

But I keep getting:

Java bytecode has not been made available to the analyzer. The org.sonar.java.bytecode.visitor.DependenciesVisitor@d678716, org.sonar.java.checks.unused.UnusedPrivateMethodCheck@58e28efd, CycleBetweenPackages rule are disabled.

And for the life of me, I can not figure out what values I need to give the sonar.java.binaries and sonar.java.test.binaries properties. I even tried using sonar.binaries, which gave me the following output:

Binary dirs: xalg.prj/h3_service_fo/build/classes

Which I did not get using either sonar.java.binaries or sonar.java.test.binaries. I also got:

JavaClasspath initialization...
sonar.binaries and sonar.libraries are deprecated since version 2.5 of sonar-java-plugin, please use sonar.java.binaries and sonar.java.libraries instead

Which is to be expected for a deprecated property. But using the sonar.java.binaries property I did not get the "Binary dirs" line in my log.

Using sonar.java.binaries:

Language is forced to java
Load rules
Load rules (done) | time=761ms
Code colorizer, supported languages: cs,plsql
Initializers : 
Base dir: D:\appl\BuildAgent\work\H3\src.prj\java.prj
Working dir: D:\appl\BuildAgent\work\H3\src.prj\java.prj\.sonar
Source paths: xalg.prj/h3_service_fo/src/main/java
Test paths: xalg.prj/h3_service_fo/src/test/java
Source encoding: windows-1252, default locale: en_US
Index files

Versus using sonar.binaries:

Language is forced to java
Load rules
Load rules (done) | time=736ms
Code colorizer, supported languages: cs,plsql
Initializers : 
Base dir: D:\appl\BuildAgent\work\H3\src.prj\java.prj
Working dir: D:\appl\BuildAgent\work\H3\src.prj\java.prj\.sonar
Source paths: xalg.prj/h3_service_fo/src/main/java
Test paths: xalg.prj/h3_service_fo/src/test/java
Binary dirs: xalg.prj/h3_service_fo/build/classes
Source encoding: windows-1252, default locale: en_US
Index files

I also looked into the source code of SonarQube, SonarQube Java Plugin and the SonarQube Scanner for instances of either "Java bytecode has not been made available to the analyzer." or sonar.java.binaries. I found plenty on sonar.java.binaries, but nothing on "Java bytecode has not been made available to the analyzer." So I have no clue what conditions exactly trigger that error.

I also tried the following permutations on sonar.java.binaries:

<property name="sonar.java.binaries" location="${project.build.dir}/classes" />
<property name="sonar.java.binaries" location="${project.build.dir}/classes/main/nl" />

But that did nothing either.

What is weird is that Squid seems to resolve the classpath just fine:

----- Classpath analyzed by Squid:
D:\appl\BuildAgent\work\H3\src.prj\java.prj\xalg.prj\h3_service_fo\build\classes\main

So, what am I missing? What am I doing wrong? Thanks in advance.

Update 2016-09-08:
Removed the entire log, the post become to long.

A subset with the (I think) relevant paths:

project.build.dir=D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj/xalg.prj/h3_service_fo/build
project.dir=D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj/xalg.prj/h3_service_fo
project.src.dir=D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj/xalg.prj/h3_service_fo/src

sonar.dir=D\:/appl/sonarqube-5.1.2
sonar.working.directory=D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj\\.sonar
sonar.projectBaseDir=D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj

sonar.jacoco.reportPath=D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj/xalg.prj/h3_service_fo/build/jacoco/test.exec
sonar.junit.reportsPath=D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj/xalg.prj/h3_service_fo/build/test-results

sonar.sources=D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj/xalg.prj/h3_service_fo/src/main/java
sonar.java.binaries=D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj\\xalg.prj\\h3_service_fo\\build\\classes\\main

sonar.java.libraries=D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj/_deploy/*.jar,D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj/_repos/lib/*.jar,D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj/_repos/provided/*.jar

sonar.tests=D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj/xalg.prj/h3_service_fo/src/test/java
sonar.java.test.binaries=D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj/xalg.prj/h3_service_fo/build/classes/test
sonar.java.test.libraries=D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj/_deploy/*.jar,D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj/_repos/lib/*.jar,D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj/_repos/provided/*.jar

The paths have exactly the same format as in my post. Could it be that the Sonar Ant runner can't figure out a path with both backslashes and slashes?

Update 2016-09-16:
Removed the entire log, the post become to long.

A subset with the (I think) relevant paths:

project.build.dir=xalg.prj\\\\h3_service_fo\\\\build
project.dir=xalg.prj\\\\h3_service_fo
project.src.dir=xalg.prj\\\\h3_service_fo\\\\src

sonar.dir=D\:\\\\appl\\\\sonarqube-5.1.2
sonar.working.directory=D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj\\.sonar
sonar.projectBaseDir=D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj

sonar.jacoco.reportPath=xalg.prj\\\\h3_service_fo\\\\build\\\\jacoco/test.exec
sonar.junit.reportsPath=xalg.prj\\\\h3_service_fo\\\\build\\\\test-results

sonar.sources=xalg.prj\\\\h3_service_fo\\\\src\\\\main\\\\java
sonar.java.binaries=D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj\\xalg.prj\\h3_service_fo\\build\\classes\\main
sonar.java.libraries=D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj\\\\_deploy\\\\*.jar,D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj\\\\_repos\\\\lib\\\\*.jar,D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj\\\\_repos\\\\provided\\\\*.jar

sonar.tests=xalg.prj\\\\h3_service_fo\\\\src\\\\test\\\\java
sonar.java.test.binaries=xalg.prj\\\\h3_service_fo\\\\build\\\\classes\\\\test
sonar.java.test.libraries=D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj\\\\_deploy\\\\*.jar,D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj\\\\_repos\\\\lib\\\\*.jar,D\:\\appl\\BuildAgent\\work\\H3\\src.prj\\java.prj\\\\_repos\\\\provided\\\\*.jar

Some paths have become relative, but I think that is because TeamCity changed the Ant file to the file in SVN. The sonar.java.binaries is absolute and it definitely points to the correct directory.

But I still get this error:

09:17:52.299 INFO  - Java Main Files AST scan done: 1579 ms
09:17:52.301 INFO  - 2/2 source files have been analyzed
09:17:52.305 WARN  - Java bytecode has not been made available to the analyzer. The org.sonar.java.bytecode.visitor.DependenciesVisitor@757a48f9, org.sonar.java.checks.unused.UnusedPrivateMethodCheck@1adf492b, CycleBetweenPackages rule are disabled.

The classpath is still interpreted just fine:

[sonar:sonar] 09:17:51.971 DEBUG - ----- Classpath analyzed by Squid:
[sonar:sonar] 09:17:51.972 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\xalg.prj\h3_service_fo\build\classes\main
[sonar:sonar] 09:17:51.973 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\batch.daemon.jar
[sonar:sonar] 09:17:51.974 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\buildinfo.jar
[sonar:sonar] 09:17:51.975 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\h2_shared.jar
[sonar:sonar] 09:17:51.975 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\h3_generator.jar
[sonar:sonar] 09:17:51.976 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\h3_loadtest.jar
[sonar:sonar] 09:17:51.977 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\h3_model_common.jar
[sonar:sonar] 09:17:51.977 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\h3_model_xalg.jar
[sonar:sonar] 09:17:51.978 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\h3_model_xalg_dao.jar
[sonar:sonar] 09:17:51.979 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\h3_model_xalg_mappers.jar
[sonar:sonar] 09:17:51.979 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\h3_model_xalg_procedures.jar
[sonar:sonar] 09:17:51.980 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\h3_model_xcare.jar
[sonar:sonar] 09:17:51.981 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\h3_model_xcare_dao.jar
[sonar:sonar] 09:17:51.982 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\h3_model_xcare_mappers.jar
[sonar:sonar] 09:17:51.982 DEBUG - D:\appl\BuildAgent\work\H3\src.prj\java.prj\_deploy\h3_model_xcare_procedures.jar

Could the Sonar Ant runner have a problem with the escaped back slashes?

Mark
  • 414
  • 1
  • 3
  • 9
  • try appending `-DsonarRunner.dumpToFile=out.txt` to the command line & examining the paths – G. Ann - SonarSource Team Sep 06 '16 at 10:04
  • Thanks for the reply, but this gave me no knew information. I've updated my post with the new information. Could you take a further look please? – Mark Sep 08 '16 at 08:04
  • I find the mix of \\ and / in your resolved paths odd. Can you try with all the slashes pointing in the same direction? – G. Ann - SonarSource Team Sep 08 '16 at 09:18
  • Did another analysis with only back slashes, but still no result. – Mark Sep 16 '16 at 07:40
  • I did try reproducing this, but I'm on Linux & I can't rule that out as a factor. I'd try: 1) removing property interpolation from your paths (just to make sure) 2) upgrading. :-/ – G. Ann - SonarSource Team Sep 23 '16 at 11:58
  • I haven't forgetting this issue, it is still on my to do list. I just haven't had time to properly do this (full sprint backlog). I will comment when I did an analyze with full paths. Upgrading is on the back log, I doubt that I can do that soonish. I will be upgrading to 5.6.3 LTS. – Mark Nov 09 '16 at 09:57
  • I upgraded to SonarQube 5.6.5 LTS. It seems as if the problem has gone away. I can't see the same error in the log anymore. How can I see if this problem really has gone away, i.e., what functionality did I gain? – Mark Jan 27 '17 at 12:44
  • Is this perhaps related to the dropped DSM features? http://blog.hello2morrow.com/2017/01/use-sonarqube-sonargraph-plugin-to-detect-cyclic-dependencies/ – Mark Jan 30 '17 at 06:42

6 Answers6

10

This is what I use, and it works.

sonar-scanner -Dsonar.projectKey=projectName
-Dsonar.gitlab.commit_sha=$CI_BUILD_REF
-Dsonar.gitlab.ref_name=$CI_BUILD_REF_NAME -Dsonar.sources=directory\src\
-Dsonar.java.binaries=.build\libs\

You need to point sonar.java.binaries to a directory that contains jars. I would't try to get fancy with wildcards and file extensions, that's not the same thing. You need a directory as an argument, not a regular expression for files.

I don't know everything about sonar scanner, but my configuration works.

Take my example into context, I use it in a gitlab runner pipeline. Your double backslashes should be fine, but you can always substitute them for forwards slashes in your config files.

peterh
  • 11,875
  • 18
  • 85
  • 108
David Hunsicker
  • 1,140
  • 3
  • 14
  • 24
  • 9
    I think you are confused about the properties. Per documentation is the property you mentioned "sonar.java.libraries", not "sonar.java.binaries". https://docs.sonarqube.org/display/PLUG/Java+Plugin+and+Bytecode – Mark Feb 16 '18 at 08:24
  • 1
    as per doc --- sonar.java.binaries = Comma-separated paths to directories containing the compiled bytecode files corresponding to your source files. AND sonar.java.libraries =Comma-separated paths to files with third-party libraries (JAR or Zip files) used by your project. Wildcards can be used: sonar.java.libraries=path/to/Library.jar,directory/**/*.jar -Dsonar.java.binaries=.build\libs\ ....so here we should have libraries and not binaries – Shashank Bodkhe Jun 17 '21 at 12:46
10

Here are some options for a Gradle multi-project. I'm running in CI (Jenkins) but the principles apply.

Options:

  1. Hand-maintain a hardcoded sonar.java.binaries entry, eg: sonar.java.binaries=subproject1/build/classes,subproject2/build/classes, (etc)

  2. Set it to the top-level directory of the project:

    sonar.java.binaries=.

  3. Use a script to generate your own props file at runtime, for example:

    cat > sonar-project.properties <<EOF
    sonar.projectKey=XXXX
    sonar.projectVersion=YYYY
    sonar.sources=$(find . -path '*/src/main' | xargs | tr ' ' ',')
    sonar.java.binaries=$(find . -path '*/build/classes/java' | xargs | tr ' ' ',')
    EOF
    

Note that the above also applies to sonar.sources, and it is likely going to need to be in sync with sonar.java.binaries. Having binaries be a superset of source seems to be OK. Obviously, if your sources value is too broad (ie includes tests, etc), you'll get more noise. The advantage of using a script is that you can customize these heuristics to your taste.

Akom
  • 1,484
  • 19
  • 25
1

IDK but adding this sonar.java.binaries= **/*.bin to sonar-project.properties helped me

Muhammet
  • 320
  • 1
  • 10
  • I can confirm that no script is needed as wildcard is working for me too ; for example if you have a maven multi-modules project with parent pom under `dev/java` and your binaries under multiple `target/classes` sub-directories, then using this is OK (notice that I'm reusing here a gitlab var)) `-Dsonar.java.binaries=${CI_PROJECT_DIR}/dev/java/**/target/classes` – boly38 Jul 20 '23 at 13:07
0

you have to provide the parameter sonar.java.libraries.

if you use maven you can add these inside the pom.xml as a property

<project>
    ...
    <properties>
        ...
        <sonar.java.libraries>target</sonar.java.libraries>

    </properties>

</project>   

or as run parameter -Dsonar.java.libraries=target

Martin Frank
  • 3,445
  • 1
  • 27
  • 47
0

Based on the documentation at https://docs.sonarqube.org/latest/analysis/languages/java/

The purpose of sonar.java.binaries path is:

Comma-separated paths to directories containing the compiled bytecode files corresponding to your source files.

For Gradle, the default path is at build/classes, so I would set:

sonar.java.binaries=build/classes
elem103
  • 179
  • 1
  • 3
-1

Solving this issue is relatively simple, if you are using Sonarcube manually then do the following. go to this webpage (https://docs.sonarsource.com/sonarqube/latest/analyzing-source-code/languages/java/)

Then on your own sonarcube localhost at port 9000, in the taskbar you will see the tab called administration then follow this path

Administration > General Settings > Languages > Java.

Then in the input tag associated with java fill in the form with sonar.java.binaries and rescan your code in your cli and it would work

  • This answer has nothing to do with the question I originally asked. I used Ant, no the UI. – Mark Aug 07 '23 at 14:29