码迷,mamicode.com
首页 > Windows程序 > 详细

项目windows运行正常,而Linux上运行报错: class path resource [kwhRules.json] cannot be resolved to absolute file path because it does not reside in the file system

时间:2020-02-25 11:10:24      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:获取   tst   sid   system   bec   val   his   this   absolute   

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 path because it does not reside in the file system

标签:获取   tst   sid   system   bec   val   his   this   absolute   

原文地址:https://www.cnblogs.com/smileblogs/p/12360576.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!