IOException解析类路径资源中的XML文档

时间:2022-12-01 11:23:30

Ok I'm currently trying mavenise a project. However my project fails to find the xml file containing the some beans. combined2.xml

好吧,我现在正在尝试mavenise项目。但是我的项目没有找到包含一些bean的xml文件。combined2.xml

I have it defined as:

我将它定义为:

    public RepeatingGrpPoC() {
    appContext = new ClassPathXmlApplicationContext(
            new String[] { "src/main/java/resources/combined2.xml",});
    c = 0;    
}

However for a reason unbeknown to me I constantly get the error.

然而,不知什么原因,我总是犯错误。

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [src/main/java/resources/combined2.xml]; nested exception is java.io.FileNotFoundException: class path resource [src/main/java/resources/combined2.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:126)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:92)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:465)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:395)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
at metadataPoC.RepeatingGrpPoC.<init>(RepeatingGrpPoC.java:34)
at metadataPoC.Main.main(Main.java:22)

Caused by: java.io.FileNotFoundException: class path resource [src/main/java/resources/combined2.xml] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:141) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328) ... 14 more

引起的:java。FileNotFoundException:类路径资源[src/main/java/resources/combined2]。不能被打开,因为它不存在于org.springframework. core.o . classpathresource.getinputstream (ClassPathResource.java:141)在org. springframework.factory.xml.xmlbeandefinitionreader . loadbeandefinition (XmlBeanDefinitionReader.java:328)…14个吧

Where else would the program be looking for this file since I've given it the relative path?

如果我已经给出了相对路径,程序还会在哪里寻找这个文件呢?

4 个解决方案

#1


28  

It is trying to load this file from the classpath and cannot find it. Try specifying just "combined2.xml" instead of "src/main/java/resources/combined2.xml" and make sure that src/main/java/resources is on your classpath.

它试图从类路径中加载这个文件,但找不到它。指定“combined2试试。xml”而不是“src /主/ java /资源/ combined2。确保src/main/java/resources在您的类路径中。

By the way, in Maven, the standard directory for resources is src/main/resources, so I suggest you put this file there.

顺便说一下,在Maven中,资源的标准目录是src/main/resources,所以我建议您将这个文件放在那里。

#2


1  

Maven, has standard directory for resources is which is src/main/resources, so if you keep your file here it will take it. and in the path simply give the file name.

Maven具有资源的标准目录,它是src/main/resources,所以如果您将文件保存在这里,它将接受它。在路径中只给出文件名。

For example

例如

 ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("application-context.xml");

I had the same problem it worked for me

我也遇到了同样的问题

#3


0  

Try this

试试这个

appContext = new ClassPathXmlApplicationContext(
            new String[] { "/**/combined2.xml", "/**/xxx.xml"});

#4


0  

You can use the relative path of the xml file.
relative path: path relative to your package where the XML file is located.

E.g. 
Assume,
package = beanfactory,  
xml file name = application-context.xml, 
and xml file in under this package.
then provide the path as "/beanfactory/application-context.xml"
ApplicationContext factory=new 
ClassPathXmlApplicationContext("/beanfactory/application-context.xml");
This works without errors.

#1


28  

It is trying to load this file from the classpath and cannot find it. Try specifying just "combined2.xml" instead of "src/main/java/resources/combined2.xml" and make sure that src/main/java/resources is on your classpath.

它试图从类路径中加载这个文件,但找不到它。指定“combined2试试。xml”而不是“src /主/ java /资源/ combined2。确保src/main/java/resources在您的类路径中。

By the way, in Maven, the standard directory for resources is src/main/resources, so I suggest you put this file there.

顺便说一下,在Maven中,资源的标准目录是src/main/resources,所以我建议您将这个文件放在那里。

#2


1  

Maven, has standard directory for resources is which is src/main/resources, so if you keep your file here it will take it. and in the path simply give the file name.

Maven具有资源的标准目录,它是src/main/resources,所以如果您将文件保存在这里,它将接受它。在路径中只给出文件名。

For example

例如

 ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("application-context.xml");

I had the same problem it worked for me

我也遇到了同样的问题

#3


0  

Try this

试试这个

appContext = new ClassPathXmlApplicationContext(
            new String[] { "/**/combined2.xml", "/**/xxx.xml"});

#4


0  

You can use the relative path of the xml file.
relative path: path relative to your package where the XML file is located.

E.g. 
Assume,
package = beanfactory,  
xml file name = application-context.xml, 
and xml file in under this package.
then provide the path as "/beanfactory/application-context.xml"
ApplicationContext factory=new 
ClassPathXmlApplicationContext("/beanfactory/application-context.xml");
This works without errors.