src2jar项目Maven clean test失败,报NoClassDefFoundError ForkedBooter
Hey there! Let's dig into why your Maven clean test is failing for the src2jar project. Based on the test code snippet you shared (using JavaCompiler and DefaultSystemFileManager), here are some common issues and actionable fixes to try:
The ToolProvider.getSystemJavaCompiler() call might fail if Maven is running on a JRE instead of a full JDK. This is a super common gotcha for tests that use the Java compiler API.
- Enforce JDK usage in Maven: Add or update the
maven-compiler-pluginin yourpom.xmlto explicitly point to your JDK path:<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <source>17</source> <!-- Match your project's JDK version --> <target>17</target> <fork>true</fork> <executable>${JAVA_HOME}/bin/javac</executable> </configuration> </plugin> </plugins> </build> - For JDK 9+: Ensure the
jdk.compilermodule is available to your tests. No extra dependencies needed here, just make sure your test JVM is a full JDK.
Your DefaultSystemFileManager isn't being closed in the test snippet, which can lead to file locks that block Maven's clean phase from deleting the target directory. Always use try-with-resources to auto-close it:
public void testOneSourceFile() throws Exception { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); // Use try-with-resources to auto-close the file manager try (DefaultSystemFileManager fileManager = new DefaultSystemFileManager( compiler.getStandardFileManager(null, null, null))) { // Your existing test logic here } }
Maven's Surefire plugin might be reusing JVM forks that hold onto resources. Force it to use fresh JVMs for tests and point to your JDK:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.2.5</version> <configuration> <jvm>${JAVA_HOME}/bin/java</jvm> <forkCount>1</forkCount> <reuseForks>false</reuseForks> </configuration> </plugin>
Instead of writing compiled classes directly to Maven's target directory (which can cause conflicts during clean), use a temporary directory for test compilation:
Path tempOutput = Files.createTempDirectory("src2jar-test"); fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(tempOutput.toFile())); // After test runs, clean up the temp directory Files.walk(tempOutput) .sorted(Comparator.reverseOrder()) .map(Path::toFile) .forEach(File::delete);
Run mvn clean test -X to generate verbose logs. Look for specific errors like:
- Compilation failures in your test code
- File lock exceptions during the
cleanphase - ClassNotFound errors for compiler API classes
These details will help pinpoint exactly what's breaking the build.
Start with checking the JDK environment and resource management first—those are the most frequent culprits for this kind of issue. If you can share the full error stack trace from the verbose log, we can narrow it down even further!
内容的提问来源于stack exchange,提问作者Grim

