项目windows运行正常,而Linux上运行报错: class path resource [kwhRules.json] cannot be resolved to absolute file
Posted Peter Jones
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了项目windows运行正常,而Linux上运行报错: class path resource [kwhRules.json] cannot be resolved to absolute file 相关的知识,希望对你有一定的参考价值。
java.io.FileNotFoundException:
class path resource [kwhRules.json]
cannot be resolved to absolute file path because it does not reside in the file system:
jar:file:/root/app/target/app.jar!/BOOT-INF/classes!/kwhRules.json
更改前代码:
void initRules() throws IOException { var file = ResourceUtils.getFile("classpath:kwhRules.json");var ob = new ObjectMapper(); this.rawRules = ob.readValue(file, Map.class); }
更改后代码:
void initRules() throws IOException {var classPathResource = new ClassPathResource("kwhRules.json"); var ob = new ObjectMapper(); this.rawRules = ob.readValue(classPathResource.getInputStream(), Map.class); }
错误原因:
ResourceUtils.getFile("classpath:config.json") 这
个方法只能从类路径下获取文件,无法从jar包中获取. 打成jar包后发布到linuxs系统,就报错找不到文件了.
ClassPathResource classPathResource = new ClassPathResource("config.json"); 可以从jar包获取文件.
以上是关于项目windows运行正常,而Linux上运行报错: class path resource [kwhRules.json] cannot be resolved to absolute file 的主要内容,如果未能解决你的问题,请参考以下文章
在PyCharm上正常运行的python程序在linux服务器上运行就报错
在PyCharm上正常运行的python程序在linux服务器上运行就报错
在PyCharm上正常运行的python程序在linux服务器上运行就报错
GreenDroid怎么用,就是怎么部署运行啥的?android