You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

1. Fix JDK/Classpath Mismatches in Test Environment

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-plugin in your pom.xml to 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.compiler module is available to your tests. No extra dependencies needed here, just make sure your test JVM is a full JDK.
2. Fix Resource Leaks in File Manager

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
    }
}
3. Configure Surefire Plugin for Isolated Test Runs

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>
4. Use Temporary Directories for Compilation Output

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);
5. Get Detailed Error Logs

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 clean phase
  • 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:55:55