您好,我在 java 应用程序中使用 src/main/resources 中的配置文件。我在类里面读它是这样的:
new BufferedReader(new FileReader(new File("src/main/resources/config.txt")));
所以现在我正在使用 mvn assembly: assembly
通过 Maven 构建它。这是我的 pom.xml 中的部分内容:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<finalName>TestSuite</finalName>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.some.package.Test</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
因此,当我运行我的应用程序时,我收到此错误:
src\main\resources\config.txt (The system cannot find the path specified)
但是当我右键单击组装好的 jar 时,我可以看到它在里面,有人知道我做错了什么吗?
最佳答案
来自 src/main/resources
的资源将被放入类路径的根目录中,因此您需要按以下方式获取资源:
new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/config.txt")));
您可以通过查看 maven 生成的 JAR/WAR 文件进行验证,因为您会在存档的根目录中找到 config.txt
。
关于java - 使用 maven 构建后未找到 src/main/resources 中的资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55990694/