[英]ClassPathResource: File not found exception when running the jar
我寫了一個如下的Java代碼
String readFile(String schemaFileName) throws IOException {
ClassPathResource classPathResource = new ClassPathResource("abc/" + schemaFileName);
return FileUtils.readFileToString(classPathResource.getFile(), StandardCharsets.UTF_8);
}
當我從IntelliJ運行時,一切正常,但是當我使用創建的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]
在我的罐子里,我可以看到目錄
$ ll BOOT-INF/classes/abc/myfile
total 44
drwxrwxr-x 2 ganesh ganesh 4096 Jan 23 12:59 ./
drwxrwxr-x 3 ganesh ganesh 4096 Jan 23 12:59 ../
-rw-rw-r-- 1 ganesh ganesh 31735 Jan 23 12:59 myfile
我想念什么?
如果spring-context.xml
和my.config
文件位於不同的jar中,則需要使用classpath*:my.config
另外,從jar文件中加載時,請確保使用的是resource.getInputStream()
而不是resource.getFile()
。
可以在這里找到更多詳細信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.