using jacoco as java agent, question on "removing those duplicated clases"

423 views
Skip to first unread message

आलोक कुमार

unread,
Jan 18, 2022, 4:52:37 AM1/18/22
to JaCoCo and EclEmma Users
From https://www.jacoco.org/jacoco/trunk/doc/faq.html

Why do I get the error "Can't add different class with same name"?

For coverage report generation all classes within a group must have unique names. You get this error during report generation if JaCoCo is supplied with multiple different class files with the same name. To fix this remove those duplicate classes or create separate reports or report groups for each version.


Now to my question.
We would like to generate code coverage reports upon regular execution in a controlled environment, rather than only via junit. 

For this the options used are

java -javaagent:"/path/to/org.jacoco.agent-0.8.7-runtime.jar -cp "${CLASS_PATH}" "${_class}"

after bringing down the java process, we run

java -jar /path/to/org.jacoco.cli-0.8.7-nodeps.jar  report ./depbase/tmp/jacoco.exec --html ./report --sourcefiles /path/to/sourcefiles --classfiles /path/to/classfiles

Leading to the output

[INFO] Loading execution data file /path/to/jacoco.exec.
Exception in thread "main" java.io.IOException: Error while analyzing path/to/classfiles/log4j-core-2.17.1.jar@org/apache/logging/log4j/core/util/SystemClock.class.
        at org.jacoco.cli.internal.core.analysis.Analyzer.analyzerError(Analyzer.java:162)
        at org.jacoco.cli.internal.core.analysis.Analyzer.analyzeClass(Analyzer.java:134)
        at org.jacoco.cli.internal.core.analysis.Analyzer.analyzeClass(Analyzer.java:157)
        at org.jacoco.cli.internal.core.analysis.Analyzer.analyzeAll(Analyzer.java:193)
        at org.jacoco.cli.internal.core.analysis.Analyzer.analyzeZip(Analyzer.java:265)
        at org.jacoco.cli.internal.core.analysis.Analyzer.analyzeAll(Analyzer.java:196)
        at org.jacoco.cli.internal.core.analysis.Analyzer.analyzeAll(Analyzer.java:226)
        at org.jacoco.cli.internal.core.analysis.Analyzer.analyzeAll(Analyzer.java:221)
        at org.jacoco.cli.internal.commands.Report.analyze(Report.java:110)
        at org.jacoco.cli.internal.commands.Report.execute(Report.java:84)
        at org.jacoco.cli.internal.Main.execute(Main.java:90)
        at org.jacoco.cli.internal.Main.main(Main.java:105)
Caused by: java.lang.IllegalStateException: Can't add different class with same name: org/apache/logging/log4j/core/util/SystemClock
        at org.jacoco.cli.internal.core.analysis.CoverageBuilder.visitCoverage(CoverageBuilder.java:106)
        at org.jacoco.cli.internal.core.analysis.Analyzer$1.visitEnd(Analyzer.java:99)
        at org.jacoco.cli.internal.asm.ClassVisitor.visitEnd(ClassVisitor.java:377)
        at org.jacoco.cli.internal.core.internal.flow.ClassProbesAdapter.visitEnd(ClassProbesAdapter.java:100)
        at org.jacoco.cli.internal.asm.ClassReader.accept(ClassReader.java:725)
        at org.jacoco.cli.internal.asm.ClassReader.accept(ClassReader.java:401)
        at org.jacoco.cli.internal.core.analysis.Analyzer.analyzeClass(Analyzer.java:116)
        at org.jacoco.cli.internal.core.analysis.Analyzer.analyzeClass(Analyzer.java:132)
        ... 10 more


The class SystemClock exists in two third party libraries, both with the package name starting with org.apache.

So, tried the same again with 

=excludes=org/apache/*

But that doesn't make the error go away.

So, coming back to the recommendation in the faq, which was 

"To fix this remove those duplicate classes or create separate reports or report groups for each version."

since there is no interest in generating code coverage or even instrumenting for the apache libraries, what's the option to use withing javaagent, other than

java -javaagent:"/path/to/org.jacoco.agent-0.8.7-runtime.jar=includes=mypackage|mypkg=excludes=^(?!.*(mypackage|mypkg)).*$:org/apache/*"

(Include my packages, exclude anything that's not my package, exclude anything that's org/apache/*)

?

Thanks in advance.

Marc Hoffmann

unread,
Jan 18, 2022, 5:38:08 AM1/18/22
to JaCoCo and EclEmma Users
Hi,

the command line tool will pick up any class files from the location path and also recursively scan zip/jar files found in any sub directory.

What you can do is to copy only the class files you’re interested in to a separate folder and let the command line tool pick-up classes from there.

Excluding classes in the Java agent will not fix your problem. This only means these classes will not be instrumented at runtime.

Regards,
-marc


--
You received this message because you are subscribed to the Google Groups "JaCoCo and EclEmma Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jacoco+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jacoco/a236d57b-e0e5-4b52-9873-3f6f319a10e0n%40googlegroups.com.

आलोक कुमार

unread,
Jan 18, 2022, 5:59:04 AM1/18/22
to JaCoCo and EclEmma Users
thanks, that worked.

How can I create a patch / raise a PR / raise an MR to update the FAQ at https://www.jacoco.org/jacoco/trunk/doc/faq.html to reflect this additional 3 sentences which would have obviated the query to begin with?

If you feel this is not important enough to merit inclusion in the FAQ please ignore my message

मंगलवार, 18 जनवरी 2022 को 4:08:08 pm UTC+5:30 बजे Marc R. Hoffmann ने लिखा:

Marc Hoffmann

unread,
Jan 18, 2022, 6:48:09 AM1/18/22
to jac...@googlegroups.com
Hi,

glad to hear!


Feel free to create a PR or just propose a improvement here in the thread.

Regards,
-marc


आलोक कुमार

unread,
Jan 19, 2022, 1:19:55 AM1/19/22
to JaCoCo and EclEmma Users
Thanks, created https://github.com/jacoco/jacoco/pull/1275
It's my first so less hazing please

मंगलवार, 18 जनवरी 2022 को 5:18:09 pm UTC+5:30 बजे Marc R. Hoffmann ने लिखा:

Aparna Bhure

unread,
Sep 14, 2022, 1:17:44 AM9/14/22
to JaCoCo and EclEmma Users
Hello Alok,
I am using below command to copy required files:

java -javaagent:/lib/jacocoagent.jar=destfile=/lib/jacoco.exec,classdumpdir=/lib/dump -jar /lib/app.jar

this is copying all files present in the JAR.
To include only selective once I am using below command

java -javaagent:/lib/jacocoagent.jar=destfile=/lib/jacoco.exec,append=true,classdumpdir=/lib/dump,includes="com.example.myservice.*" -jar /lib/app.jar

This is copying only files present in the required package

Now I am generating report using below command:

java -jar /lib/jacococli.jar report /lib/jacoco.exec --classfiles /lib/dump/ --html lib/report

this is generating report only for the classes present in the given package but it is not redirecting to the actual java class to highlight the code. I saw in reports folder there is no MyClass.java.html file got generated via jacococli command

where as when try to generate report using maven command it gets generated and redirecting to the class.

 mvn org.jacoco:jacoco-maven-plugin:0.8.8:report -Djacoco.dataFile=/lib/jacoco.exec

Can you help here? looks like you are also using the same command, except the JAR name is different
Reply all
Reply to author
Forward
0 new messages