抛出的错误文件即使存在,也不存在 [英] Error thrown file does not exist even though it is there
问题描述
这里是我的代码结构:
这里是从DAO插入代码:
public void insert(String name){
Resource r = new ClassPathResource ( app.xml中);
BeanFactory factory = new XmlBeanFactory(r);
EmployeeDao dao =(EmployeeDao)factory.getBean(d);
员工e =新员工();
e.setId(114);
e.setName(name);
e.setSalary(50000);
dao.saveEmployee(e);
}
我得到这个错误:
出现意外错误(type = Internal Server Error,status = 500)。
IOException从类路径资源[app.xml]解析XML文档;嵌套异常是java.io.FileNotFoundException:类路径资源[app.xml]无法打开,因为它不存在。
我尝试关闭项目并打开它,清理它并构建它,但没有任何工作。请指教。
您只需指定 app.xml的路径
pre $ 资源r = new ClassPathResource(main / java / db / app.xml);
错误原因:资源由 ClassLoader.getResourceAsStream( )
,而不是由 Class.getResourceAsStream()
。
同样的原因是转发 /
缺席。
Here's my code structure :
And here's the insert code from DAO :
public void insert(String name) {
Resource r=new ClassPathResource("app.xml");
BeanFactory factory=new XmlBeanFactory(r);
EmployeeDao dao=(EmployeeDao)factory.getBean("d");
Employee e=new Employee();
e.setId(114);
e.setName(name);
e.setSalary(50000);
dao.saveEmployee(e);
}
I get this error:
There was an unexpected error (type=Internal Server Error, status=500). IOException parsing XML document from class path resource [app.xml]; nested exception is java.io.FileNotFoundException: class path resource [app.xml] cannot be opened because it does not exist`.
I tried closing the project and opening it, cleaning it and building it, but nothing works. Please advice.
You just need to specify the path to the app.xml
Resource r = new ClassPathResource("main/java/db/app.xml");
The reason of the error: the resource is loaded by ClassLoader.getResourceAsStream()
, not by Class.getResourceAsStream()
.
The same reason is for a forward /
absence.
这篇关于抛出的错误文件即使存在,也不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!