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

    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包获取文件.

  • 相关阅读:
    css优化篇
    select超全超详细总结篇
    meta总结
    富文本编辑
    textarea 换行处理
    07 DRF响应类:Response
    06 内部类
    05 序列化组件
    04 APIView的请求生命周期
    python中if __name__ == '__main__'是什么?
  • 原文地址:https://www.cnblogs.com/smileblogs/p/12360576.html
Copyright © 2011-2022 走看看