***.jar!\BOOT-INF\classes!\***.xml没有此文件

***.jar!\BOOT-INF\classes!\***.xml没有此文件

问题描述

最近遇见一个问题,在开发环境下正常运行的代码,打包后不能正常运行,由于日志被定制化了,问题很难定位,经过细(坑)心(爹)的排查,终于找到问题——配置文件没有读取。纳尼!!

原因

这主要是因为jar包是一个单独的文件而不是文件夹,不能通过“file:***.jar!\BOOT-INF\classes!\jdbcType.xml”定位jar包内的资源。

解决

读取本地文件如下所示:

SAXReader xmlReader = new SAXReader();
String path = URLDecoder.decode(this.getClass().getResource("/").getPath(), "UTF-8");
File xmlFile = new File(path + "/jdbcType.xml");
Document document = xmlReader.read(xmlFile);

改为

SAXReader xmlReader = new SAXReader();
InputStream xmlFile = this.getClass().getResourceAsStream("/jdbcType.xml");
Document document = xmlReader.read(xmlFile);

猜你喜欢

转载自blog.csdn.net/weixin_43229107/article/details/85318551