Offline Instrumentation

One of the main benefits of JaCoCo is the Java agent, which instruments classes on-the-fly. This simplifies code coverage analysis a lot as no pre-instrumentation and classpath tweaking is required. However, there can be situations where on-the-fly instrumentation is not suitable, for example:

For such scenarios class files can be pre-instrumented with JaCoCo, for example with the instrument Ant task. At runtime the pre-instrumented classes needs be on the classpath instead of the original classes. In addition jacocoagent.jar must be put on the classpath.

Configuration

In offline mode the JaCoCo runtime can be configured with the same set of properties which are available for the agent, except for the includes/excludes options as the class files are already instrumented. There are two different ways to provide the configuration:

In both cases configuration values may contain variables in the format ${name} which are resolved with system property values at runtime. For example:

destfile=${user.home}/jacoco.exec

Class Loading and Initialization

Unlike with on-the-fly instrumentation offline instrumented classes get a direct dependency on the JaCoCo runtime. Therefore jacocoagent.jar has to be on the classpath and accessible by the instrumented classes. The proper location for jacocoagent.jar might depend on your deployment scenario. The first instrumented class loaded will trigger the initialization of the JaCoCo runtime. If no instrumented class is loaded the JaCoCo runtime will not get started at all.

Using Pre-Instrumented Classes With the Java Agent

It is possible to also use offline-instrumented classes with the JaCoCo Java agent. In this case the configuration is taken from the agent options. The agent must be configured in a way that pre-instrumented classes are excluded, e.g. with "excludes=*". Otherwise it will result in error messages on the console if the agent instruments such classes again.

Execution Data Collection

If jacocoagent.jar is used on the classpath it will collect execution data the same way as used as a Java agent. Depending on the output configuration execution data can be collected via a remote connection or is written to the file system when the JVM terminates. For the latter it is required that e.g. a java task is executed with fork="true".

Report Generation

Based on the collected *.exec files reports can be created the same way as for execution data collected with the Java agent. Note that for report generation the original class files have to be supplied, not the instrumented copies.