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

如何配置Java,使其编译器从test-classes文件夹读取指定XSD文件?

Hey there! Let's break down how to adjust your setup so your program reads the XSD file from the test-classes folder instead of target when running the JAR.

解决方案:让程序从test-classes读取XSD文件

First, a quick context check: test-classes is the directory where build tools like Maven store test classes and test resources, and it's not included in the main JAR by default. To make your program access files here, we'll cover both build configuration tweaks and code adjustments:

1. Adjust Maven Build Configuration (if using Maven)

If your project uses Maven, the main JAR only includes content from src/main/resources and target/classes by default. Here are two ways to make test-classes resources accessible:

  • Option 1: Package test resources into the main JAR
    Add this maven-jar-plugin config to your pom.xml to include the XSD file from test-classes into your main JAR:

    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.3.0</version>
          <configuration>
            <additionalResources>
              <resource>
                <directory>${project.build.testOutputDirectory}</directory>
                <includes>
                  <include>**/*.xsd</include>
                </includes>
              </resource>
            </additionalResources>
          </configuration>
        </plugin>
      </plugins>
    </build>
    

    After building, the XSD will live inside the JAR, so your program won't depend on the external test-classes folder anymore.

  • Option 2: Add test-classes to the classpath at runtime
    If you don't want to modify the JAR content, just append test-classes to the classpath when running the JAR:

    # Linux/macOS
    java -cp your-app.jar:target/test-classes com.your.package.MainClass
    
    # Windows
    java -cp your-app.jar;target/test-classes com.your.package.MainClass
    

    The JVM will scan all directories in the classpath, so it'll pick up the XSD from test-classes.

2. Fix How Your Code Reads the Resource

No matter which build config you use, the way your code fetches the XSD matters a lot. Always use classpath-based reading instead of hardcoding absolute paths:

  • ❌ Bad (depends on absolute file path):
    File xsdFile = new File("target/your-schema.xsd");
    
  • ✅ Good (reads from classpath):
    // Use this in main classes
    InputStream xsdStream = getClass().getClassLoader().getResourceAsStream("your-schema.xsd");
    
    // Or this in test classes (leading slash ensures root classpath lookup)
    InputStream xsdStream = getClass().getResourceAsStream("/your-schema.xsd");
    
    This way, the program will find the XSD as long as it's present in any directory on the classpath (including test-classes).

3. For Test-Specific JARs

If you're building a JAR specifically to run tests, use Maven's test-jar goal to package test classes and resources:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>3.3.0</version>
  <executions>
    <execution>
      <goals>
        <goal>test-jar</goal>
      </goals>
    </execution>
  </executions>
</plugin>

When you run this test JAR, the classpath will automatically include test-classes content, so your XSD will be found without extra steps.


内容的提问来源于stack exchange,提问作者Matt

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:40:33