Backend Development 12 min read

Using JaCoCo to Analyze Online Java Code Coverage and Eliminate Zombie Code

This article explains how to use JaCoCo probes to analyze online Java code execution, identify and remove zombie code, and improve development efficiency through detailed steps including Maven dependencies, a REST dump endpoint, javaagent configuration, script automation, and coverage analysis results.

JD Tech
JD Tech
JD Tech
Using JaCoCo to Analyze Online Java Code Coverage and Eliminate Zombie Code

As business systems evolve rapidly, large amounts of legacy or "zombie" code often remain in production, increasing maintenance costs and hindering development efficiency. By leveraging JaCoCo probes, teams can obtain precise runtime coverage data, pinpoint unused functionalities, and safely decommission or delete dead code.

The proposed solution consists of several concrete measures:

4.1 Add JaCoCo Ant Dependency <dependency> <groupId>org.jacoco</groupId> <artifactId>org.jacoco.ant</artifactId> <version>0.8.3</version> </dependency> <dependency> <groupId>org.apache.ant</groupId> <artifactId>ant</artifactId> <version>1.9.9</version> </dependency>

4.2 Expose a REST Dump Endpoint @RestController @RequestMapping("/coverage") public class CoverageController { @PostMapping("dump") @NoCheckMenuPermission public Result dumpCoverageFile() { DumpTask dumpTask = new DumpTask(); // dump file location dumpTask.setDestfile(new File("/export/Data/coverage/code-cover.exec")); // append on multiple dumps dumpTask.setAppend(true); // choose an idle port dumpTask.setPort(8840); // default to localhost dumpTask.setAddress("127.0.0.1"); dumpTask.execute(); return Result.succeed(true); } }

4.3 Embed JaCoCo Agent Decompress the agent JAR to avoid involving operations personnel: jar -xvf $BASEDIR/lib/org.jacoco.agent-0.8.3.jar Add the javaagent parameter to the startup script: -javaagent:$BASEDIR/bin/jacocoagent.jar=includes=com.jdwl.*,output=tcpserver,port=8840,address=127.0.0.1 -Xverify:none

4.4 Reserve JDOS Resources (custom export directory) – see JDOS documentation for details.

4.5 Clean Export Directory #! /bin/bash ls -lh /export | awk 'NR >1 {print}' | awk '{if ($9 != "Data") print $9}' | xargs -i /bin/rm -rf /export/{} > /dev/null 2>&1 ls -lh /export/Data | awk 'NR >1 {print}' | awk '{if ($9 != "jdos.jd.com" && $9 != "coverage") print $9}' | xargs -i /bin/rm -rf /export/Data/{} > /dev/null 2>&1

4.6 Download and Analyze Coverage File – after dumping, retrieve /export/Data/coverage/code-cover.exec via bastion host and use IDE (e.g., IntelliJ) to view coverage data.

The practical outcomes are significant: delivery cycle shortened from ~15 to ~12 days, development stage duration reduced by ~31%, duplicate code blocks decreased from 31 to 27, per‑person demand throughput rose from 1.5 to 2.5, and unit test line coverage improved from 51.33% to 52.28%.

References

1. JaCoCo Documentation – https://www.jacoco.org/jacoco/trunk/doc/index.html 2. Java Agent Guide – https://www.cnblogs.com/rickiyang/p/11368932.html 3. Jacoco Coverage Practice – https://www.cnblogs.com/ccoder/p/15369719.html#4946710 4. Bytecode Manipulation with ASM/Javassist – https://newrelic.com/blog/best-practices/java-performance-monitoring

BackendJavaCode CoverageAutomationJaCoCo
JD Tech
Written by

JD Tech

Official JD technology sharing platform. All the cutting‑edge JD tech, innovative insights, and open‑source solutions you’re looking for, all in one place.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.