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

含TXT文件的Maven项目打包JAR后找不到文件,如何修改pom.xml解决

Absolutely! You can fix this by adjusting your pom.xml to ensure those TXT files get included in your JAR during the build process. Here's how to do it step by step:

Why the issue happens

Maven’s default setup only includes files from the src/main/resources directory in the final JAR. Since your TXT files are stored in src/text/__files/, they’re being left out entirely—hence the "file not found" error when running the JAR.

Step 1: Update your pom.xml to include the custom resource directory

Add a <resources> section inside the <build> block to tell Maven to package files from your src/text/__files/ directory. Make sure to keep the default src/main/resources included too, in case you have other resources there:

<build>
    <!-- Add this resources section to include your TXT files -->
    <resources>
        <resource>
            <directory>src/text/__files</directory>
            <includes>
                <include>**/*.txt</include>
            </includes>
        </resource>
        <!-- Keep the default resources directory -->
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>

    <!-- Your existing maven-assembly-plugin configuration stays here -->
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.mycompany.mavenproject3.SSBStub</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Step 2: Fix how you read the files in your code

Even after packaging the files correctly, you can’t use a regular File object to access them inside a JAR. Instead, use the class loader to get an input stream:

// Example: If your file is named test.txt in src/text/__files/
InputStream txtStream = getClass().getClassLoader().getResourceAsStream("test.txt");

// If the file is in a subfolder under __files, e.g., src/text/__files/data/test.txt
InputStream txtStream = getClass().getClassLoader().getResourceAsStream("data/test.txt");

This works because the class loader looks inside the JAR’s contents for the resource path.

Verify the fix

Run mvn clean package again, then check the generated JAR (the one with -with-dependencies in the name) using a tool like jar tf your-jar-name.jar—you should see your TXT files listed there. Run the JAR again, and the file not found error should be gone.

内容的提问来源于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 07:11:31