我是靠谱客的博主 野性画笔,这篇文章主要介绍springboot打成jar后无法读取根路径和文件的解决,现在分享给大家,希望可以做个参考。

springboot打成jar后无法读取根路径和文件

ClassLoader.getSystemResourceAsStream(authenticationFileName)
PropertiesUtils.class.getClass().getResourceAsStream("/authentication.properties")

未打包时都可以获取到根路径和文件

打包后报java.lang.NullPointerException

ClassPathResource resource = new ClassPathResource("application.yml");
InputStream inputStream = resource.getInputStream();

这是因为打包后Spring试图访问文件系统路径,但无法访问JAR中的路径。

因此必须使用resource.getInputStream()

springboot打jar找不到资源文件

需要采用这种写法:

ClassPathResource resource = new ClassPathResource(filePath);
InputStream inputStream = resource.getInputStream();

这样就可以获取到了。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持靠谱客。

最后

以上就是野性画笔最近收集整理的关于springboot打成jar后无法读取根路径和文件的解决的全部内容,更多相关springboot打成jar后无法读取根路径和文件内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(272)

评论列表共有 0 条评论

立即
投稿
返回
顶部