问题备忘: 将工程打包成jar包运行,就报java.io.FileNotFoundException: class path resource错误

栏目: Java · 发布时间: 6年前

内容简介:最近在在使用@Value注入文件碰到如下问题: 工程在在IntelliJ IDEA开发环境里正常运行,但是一旦将工程打包成jar包运行,就报java.io.FileNotFoundException: class path resource错代码如下:通过@Value将resource目录下test/billingconfig-file.xml目录注入到Resource上代码在IntelliJ IDEA开发环境里正常运行,但是一旦将工程打包成jar包运行,就提示如下错误:

最近在在使用@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.xml
	at 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())
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

云计算安全与隐私

云计算安全与隐私

Tim Mather、Subra Kumaraswamy、Shahed Latif / 刘戈舟、杨泽明、刘宝旭 / 机械工业出版社华章公司 / 2011-6 / 65.00元

《云计算安全与隐私》可以使你明白当把数据交付给云计算时你所面临的风险,以及为了保障虚拟基础设施和网络应用程序的安全可以采取的行动。本书是由信息安全界知名专家所著,作者在书中给出许多中肯的忠告和建议。本书的读者对象包括:IT职员、信息安全和隐私方面的从业人士、业务经理、服务提供商,以及投资机构等。阅读本书你会了解直到现在还严重匮乏的云计算安全方面的详尽信息。 《云计算安全与隐私》主要内容包括:......一起来看看 《云计算安全与隐私》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器