項目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包獲取文件.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM