学无先后,达者为师

网站首页 java综合 正文

springboot打成jar后无法读取根路径和文件的解决_java

作者:任我行哟   更新时间: 2022-02-11 java综合

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();

这样就可以获取到了。

原文链接:https://blog.csdn.net/weixin_39986856/article/details/81271460

栏目分类
最近更新