JaCoCo Agent provides programmatic access to the JaCoCo runtime agent JAR file for Java code coverage analysis.
npx @tessl/cli install tessl/maven-org-jacoco--org-jacoco-agent@0.8.0JaCoCo Agent provides programmatic access to the JaCoCo runtime agent JAR file for Java code coverage analysis. This module serves as a wrapper and resource provider for the jacocoagent.jar file, offering APIs to extract, access, and deploy the coverage agent in various Java environments.
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<version>0.8.13</version>
</dependency>import org.jacoco.agent.AgentJar;
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;import org.jacoco.agent.AgentJar;
import java.io.File;
import java.io.IOException;
// Get the agent JAR as a URL
URL agentUrl = AgentJar.getResource();
// Get the agent JAR as an InputStream
InputStream agentStream = AgentJar.getResourceAsStream();
// Extract agent to a temporary location
File tempAgent = AgentJar.extractToTempLocation();
// Extract agent to a specific location
File specificLocation = new File("/path/to/jacocoagent.jar");
AgentJar.extractTo(specificLocation);Access the embedded JaCoCo agent JAR file as a resource.
/**
* Returns a URL pointing to the JAR file.
* @return URL of the JAR file
*/
public static URL getResource();
/**
* Returns the content of the JAR file as a stream.
* @return content of the JAR file
*/
public static InputStream getResourceAsStream();Usage Examples:
// Access via URL
URL agentUrl = AgentJar.getResource();
InputStream stream = agentUrl.openStream();
// Direct stream access with proper resource management
try (InputStream agentStream = AgentJar.getResourceAsStream()) {
// Use the stream for processing
// Stream is automatically closed when try block exits
}Extract the embedded agent JAR to file system locations.
/**
* Extract the JaCoCo agent JAR and put it into a temporary location. This
* file should be deleted on exit, but may not if the VM is terminated
* @return Location of the Agent Jar file in the local file system. The file
* should exist and be readable.
* @throws IOException Unable to unpack agent jar
*/
public static File extractToTempLocation() throws IOException;
/**
* Extract the JaCoCo agent JAR and put it into the specified location.
* @param destination Location to write JaCoCo Agent Jar to. Must be writeable
* @throws IOException Unable to unpack agent jar
*/
public static void extractTo(File destination) throws IOException;Usage Examples:
// Extract to temporary location (automatically deleted on JVM exit)
File tempAgentFile = AgentJar.extractToTempLocation();
System.out.println("Agent extracted to: " + tempAgentFile.getAbsolutePath());
// Extract to specific location
File agentFile = new File("./jacocoagent.jar");
try {
AgentJar.extractTo(agentFile);
System.out.println("Agent extracted to: " + agentFile.getAbsolutePath());
} catch (IOException e) {
System.err.println("Failed to extract agent: " + e.getMessage());
}public final class AgentJar {
// Private constructor - cannot be instantiated
private AgentJar();
// All methods are static
}The JaCoCo Agent API uses two main types of exceptions:
/jacocoagent.jar resource is not found. This typically indicates a build or packaging issue. The error includes a reference to /org.jacoco.agent/README.TXT for troubleshooting details.Error Handling Example:
try {
// Resource access - may throw AssertionError
URL agentUrl = AgentJar.getResource();
// File extraction - may throw IOException
File agentFile = new File("./jacocoagent.jar");
AgentJar.extractTo(agentFile);
} catch (AssertionError e) {
System.err.println("Agent resource not found. Check build configuration.");
} catch (IOException e) {
System.err.println("Failed to extract agent: " + e.getMessage());
}/jacocoagent.jar resource within the JAR fileCommon usage patterns for integrating JaCoCo Agent in applications:
Testing Framework Integration:
// Extract agent for use with JVM arguments
File agent = AgentJar.extractToTempLocation();
String javaagentArg = "-javaagent:" + agent.getAbsolutePath();
// Use javaagentArg when launching test JVMsBuild Tool Integration:
// Extract to build directory for distribution
File buildDir = new File("target/jacoco");
buildDir.mkdirs();
File agentJar = new File(buildDir, "jacocoagent.jar");
AgentJar.extractTo(agentJar);Runtime Deployment:
// Use the built-in extraction method for deployment
File deploymentFile = new File("/path/to/deployment/jacocoagent.jar");
AgentJar.extractTo(deploymentFile);
// The extractTo method handles stream management and proper copying