title: Jacoco maven plugin clogs up console with Exceptions-java.lang.IllegalStateException: class is already instrumented
body: <p>I am working on sonar code coverage using Jacoco plugin and using power mock mockito combination to write JUnit test cases, while all goes well with the build when I run mvn clean install but the console shows a very very long stack trace chain which keeps coming for many many classes used in the project, and is quite irritating, 
  The exception trace is similar to below- </p>
  
  <pre><code>java.lang.instrument.IllegalClassFormatException: Error while instrumenting class..
  Caused by: java.io.IOException: Error while instrumenting class.
  Caused by: java.lang.IllegalStateException: Class &lt;class-name&gt; is already instrumented.
  </code></pre>
  
  <p>I went through below link for a solution but could not get a way out:- </p>
  
  <pre><code>https://github.com/jacoco/jacoco/issues/32
  </code></pre>
  
  <p>The link says "The error indicates that there are two JaCoCo agents configured for the same process, Looks like activation of surefire-report-plugin leads to a <strong>doubled</strong> agent, which is not an exceptional case, but an error in configuration of JVM for tests" In my case it is not even doubled agent I guess as my argLine has set to only one jacoco agent.</p>
  
  <p>In my case, the argLine is set to below- </p>
  
  <pre><code>javaagent:C:\\Users\\user\\.m2\\repository\\org\\jacoco\\org.jacoco.agent\\0.7.6.201602180812\\org.jacoco.agent-0.7.6.201602180812-runtime.jar=destfile=C:Users\\user\\git\\package\\target\\jacoco.exec
  </code></pre>
  
  <p>My pom.xml entry for jacoco-sonar configuration is as below - </p>
  
  <pre><code>&lt;properties&gt;
          &lt;sonar.sources&gt;src/main/java&lt;/sonar.sources&gt;
          &lt;sonar.tests&gt;src/test/java&lt;/sonar.tests&gt;
          &lt;sonar.java.coveragePlugin&gt;jacoco&lt;/sonar.java.coveragePlugin&gt;
          &lt;sonar.jacoco.reportPaths&gt;${project.basedir}/target/jacoco.exec&lt;/sonar.jacoco.reportPaths&gt;
          &lt;sonar.language&gt;java&lt;/sonar.language&gt;
          &lt;sonar.binaries&gt;${project.basedir}/target/classes&lt;/sonar.binaries&gt;
          &lt;sonar.inclusions&gt;
              **/com/abc/service/impl/ABCServiceImpl.java,
              **/com/abc/dao/impl/ABCDAOImpl.java
          &lt;/sonar.inclusions&gt;
      &lt;/properties&gt;
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.20&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;skipTests&gt;false&lt;/skipTests&gt;
                      &lt;argLine&gt;-Xmx1024m -XX:MaxPermSize=256m ${argLine}&lt;/argLine&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.jacoco&lt;/groupId&gt;
                  &lt;artifactId&gt;jacoco-maven-plugin&lt;/artifactId&gt;
                  &lt;version&gt;0.7.6.201602180812&lt;/version&gt;
                  &lt;executions&gt;
                   &lt;execution&gt;
                          &lt;id&gt;default-prepare-agent&lt;/id&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;prepare-agent&lt;/goal&gt;
                          &lt;/goals&gt;
                  &lt;/execution&gt; 
                  &lt;execution&gt;
                      &lt;id&gt;default-instrument&lt;/id&gt;
                      &lt;goals&gt;
                          &lt;goal&gt;instrument&lt;/goal&gt;
                      &lt;/goals&gt;
                  &lt;/execution&gt;
                  &lt;execution&gt;
                      &lt;id&gt;default-restore-instrumented-classes&lt;/id&gt;
                      &lt;goals&gt;
                          &lt;goal&gt;restore-instrumented-classes&lt;/goal&gt;
                      &lt;/goals&gt;
                  &lt;/execution&gt;
                      &lt;execution&gt;
                          &lt;id&gt;default-report&lt;/id&gt;
                          &lt;phase&gt;prepare-package&lt;/phase&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;report&lt;/goal&gt;
                          &lt;/goals&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  </code></pre>
  
  <p><strong>Note</strong>: If I remove the execution element for 'default-instrument' and 'default-restore-instrumented-classes' than the exception trace does not come but I get coverage = 0.0% on sonar dashboard, using these two execution elements give me correct code coverage but with lengthy stack trace on the console. Any help is appreciated. </p>