ClassPathResource:运行jar时找不到文件异常

时间:2018-01-23 13:41:34

标签: java spring-boot resources classpath

我写了一个如下所示的java代码

select top 1  rc.XmlContent.value('count(//a/b)', 'int') from Table rc

当我从IntelliJ运行时,一切正常,但是当我使用创建的jar部署应用程序时,它会给出以下异常

String readFile(String schemaFileName) throws IOException {
    ClassPathResource classPathResource = new ClassPathResource("abc/" + schemaFileName);
    return FileUtils.readFileToString(classPathResource.getFile(), StandardCharsets.UTF_8);
}

在我的jar中,我可以看到目录

Caused by: java.io.FileNotFoundException: class path resource [abc/myfile] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/my_application.jar!/BOOT-INF/classes!/abc/myfile
    at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:215) ~[spring-core-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]

我错过了什么?

1 个答案:

答案 0 :(得分:0)

如果您的classpath*:my.configspring-context.xml文件位于不同的jar中,则需要使用my.config

另外,从jar文件中加载时,请确保使用resource.getInputStream()而不是resource.getFile()

可以找到更多详细信息here

相关问题