Java maven项目试图从资源中读取文件
我试图从maven web项目的resources文件夹中读取xml文件,但它没有试图从源文件中读取xml文件,而是尝试从web-INF文件夹中生成的jar文件中读取xml文件,这会引发一个not found异常。我如何解决这个问题?谢谢Java maven项目试图从资源中读取文件,java,maven,file,resources,web-inf,Java,Maven,File,Resources,Web Inf,我试图从maven web项目的resources文件夹中读取xml文件,但它没有试图从源文件中读取xml文件,而是尝试从web-INF文件夹中生成的jar文件中读取xml文件,这会引发一个not found异常。我如何解决这个问题?谢谢 ClassLoader classLoader = getClass().getClassLoader(); File file = new File(classLoader.getResource("folder_x/file.xml").getFile()
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("folder_x/file.xml").getFile());
在参考资料下,我有文件夹_x,其中有file.xml
如果我跑步:
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("/folder_x/file.xml").getFile());
我得到空指针异常
pom.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="maven.apache.org/POM/4.0.0"; xmlns:xsi="w3.org/2001/XMLSchema-instance"; xsi:schemaLocation="maven.apache.org/POM/4.0.0 maven.apache.org/xsd/maven-4.0.0.xsd">;
<modelVersion>4.0.0</modelVersion>
<groupId>xmltest</groupId>
<artifactId>xmltest</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
;
4.0.0
xmltest
xmltest
1.0-快照
请发布pom.xml 4.0.0 xmltest 1.0-SNAPSHOT您的资源在哪里?请发布完整的路径src/main/resources/folder\u xYou不应尝试通过文件访问资源。您应该使用类似于getClass().getResourcesAsStream(“/dolder\u x/file.xml”)
的流,因为这将在稍后打包到jar文件中。。。