问题备忘: 将工程打包成jar包运行,就报java.io.FileNotFoundException: class path resource错误
最近在在使用@Value注入文件碰到如下问题:
工程在在IntelliJ IDEA开发环境里正常运行,但是一旦将工程打包成jar包运行,就报java.io.FileNotFoundException: class path resource错
代码如下:通过@Value将resource目录下test/billingconfig-file.xml目录注入到Resource上
@Value("classpath:test/billingconfig-file.xml")
private Resource resourceFile; // 注入计费点文件资源JAXBContext context = JAXBContext.newInstance(BillingModelList.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
BillingModelList billingModelList = (BillingModelList) unmarshaller.unmarshal(resourceFile.getFile());
代码在IntelliJ IDEA开发环境里正常运行,但是一旦将工程打包成jar包运行,就提示如下错误:
java.io.FileNotFoundException: class path resource [test/billingconfig-file.xml] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/C:/Users/Administrator/Desktop/tmp/other/sp-bap-simulator-1.5.17.RELEASE.jar!/BOOT-INF/classes!/test/billingconfig-file.xmlat org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:215)at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:52)at im.yixin.spbap.logic.support.PayContext.init(PayContext.java:63)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:497)at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366)at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311)
原因是因为unmarshaller.unmarshal传入的参数是File,
在IntelliJ IDEA里运行时,billingconfig-file.xml是个独立的文件,可以被File访问到,
但是将工程打包成jar包运行,billingconfig-file.xml被封装到jar包中了,不是一个独立文件了,此时肯定无法使用File访问到。
了解了原因,我们可以将传递unmarshaller.unmarshal()方法的参数从File变成InputStream就可以了
将
unmarshaller.unmarshal(resourceFile.getFile());
修改为:
unmarshaller.unmarshal(resourceFile.getInputStream());
完整代码如下:
@Value("classpath:test/billingconfig-file.xml")
private Resource resourceFile; // 注入计费点文件资源JAXBContext context = JAXBContext.newInstance(BillingModelList.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
BillingModelList billingModelList = (BillingModelList) unmarshaller.unmarshal(resourceFile.getInputStream())
问题备忘: 将工程打包成jar包运行,就报java.io.FileNotFoundException: class path resource错误相关推荐
- 使用Eclipse将包含第三方jar库的java工程打包成jar包
1.MANIFEST.MF 在工程下新建一个MANIFEST.MF的文件,项目结构如下. M.java package cn.hwd.main; import java.io.IOException; ...
- Springboot打包成jar包后启动报错It was loaded from the following location
之前在使用springboot开发系统的时候,用到了@Resouse的注解,在IDEA运行时没有发生任何异常,但是打包部署的时候就报jar包冲突. 09:07:51.234 [main] ERROR ...
- springboot打包成jar包后找不到xml,找不到主类的解决方法
springboot打包成jar包后找不到xml,找不到主类的解决方法 参考文章: (1)springboot打包成jar包后找不到xml,找不到主类的解决方法 (2)https://www.cnbl ...
- eclipse中简单实现自己的工具类打包成jar包并在项目中使用
在现实开发中我们经常会自己写工具方便别人使用,所以就要将我们写好的工具打包成jar包,给别人调用 一.首先,编写一个类MyFristJar.java,代码如下: package com.zl.myja ...
- 将Spring Boot项目打包成jar包war包
任务一:将Spring Boot项目打包成jar包 1.在pom.xml文件中添加依赖 2.通过cmd命令行来进行打包jar包(首先进入项目的目录中) 3.进入项目中的target目录下查看包 4.使 ...
- linux上jar包无法识别本地文件格式,解决使用maven将项目打包成jar包后运行在linux系统中无法读取jar包中文件的问题...
哈哈,之前计划的每周至少更新一个博客的计划又因为各种原因都泡汤了.看来坚持真得是一件挺让人头疼的问题,无论如何,从这周开始接下来每周一定要至少更新一个技术博客,欢迎大家监督! 好,言归正传.之前前几周 ...
- [JAR包] android引入JAR包,打包成JAR包,打包成Library项目,导入Library项目
(1)项目导入JAR包: 1.在项目目录里建立一个libs目录,将外部jar包拷贝在里面. 2.右键点击项目,Bulid Path->Configure Build Path 3.在设置Libr ...
- java打包拆包_[Java] Java 打包成jar包 和 解压jar包
解压jar包 jar xf xxx.jar 打包成jar包 方法一:通过jar命令 jar命令的用法: 下面是jar命令的帮助说明: 用法:jar {ctxui}[vfm0Me] [jar-file] ...
- 在IDEA中将SpringBoot项目打包成jar包的方法 不要用 在上面有可以用的
在IDEA中将SpringBoot项目打包成jar包的方法 2018年03月07日 10:43:52 叶叶叶叶大爷 阅读数 71375 版权声明: https://blog.csdn.net/qq_3 ...
最新文章
- 《JAVA练习题目7》 定义一个素数生成器类PrimeGenerator,用于生成给定区间内的所有素数。(类PrimeGenerator都由类Main代替)
- DButils数据库升级不丢失数据
- Google Guava Cache 移除监听器
- Spring和SpringMVC的总结
- Memcached安装
- Roguelike+RPG如何给玩家刺激的游戏体验? 《我功夫特牛》系统逆推
- subversion安装与配置备忘录
- socket error:10053
- w3cschool离线手册
- 通过Kali linux 模拟CC攻击进行WEB压力测试实战
- MATLAB函数速查手册
- 网游加速器的工作原理
- 多个Gbase集群间表同步思路
- AD转换器输入之前接一个电压跟随器是为什么
- iOS 出现:不受信任的开发者 弹框
- 写QT串口通信的UI界面时,数据输入文本框为什么要有文本(字符串)发送和十六进制发送?
- 8 - 1 - Non-linear Hypotheses (10 min)
- Windows用户的分类
- openwrt - 入门( uHTTPd, opkg, uci, luci, mtd等)
- 【无标题】8421码,5421码,2421码,余三码之间的区别及对数的表示规则