'IOException parsing XML document from class path resource

Ok I'm currently trying mavenise a project. However my project fails to find the xml file containing the some beans. 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 unknown 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.(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:93) at metadataPoC.RepeatingGrpPoC.(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

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



Solution 1:[1]

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.

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

Solution 2:[2]

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.

For example

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

I had the same problem it worked for me

Solution 3:[3]

You have to replace your .xml in resources folder, and write:

 String[] contextPaths = new String[] {"Xxx.xml"};
    new ClassPathXmlApplicationContext(contextPaths);

If you didn't any addition configuration, all .html and .xml files Spring searches in resources folder by dafault

Solution 4:[4]

Keep .xml files in the resources folder and use ClassPathXmlApplicationContext like this:

Example:

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

Solution 5:[5]

Try this

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

Solution 6:[6]

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.

Solution 7:[7]

Move combined2.xml to your resources folder.

If you want to put any other config file like applicationContext.xml, make sure you put it in resources folder.

Solution 8:[8]

I got it.. correct answer

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

Solution 9:[9]

Put the Config file under Resource folder if you are using Maven. Like below.

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dogbane
Solution 2 Varun
Solution 3
Solution 4 seshadri_c
Solution 5 Anand Devaraj
Solution 6 tripleee
Solution 7 Noah Broyles
Solution 8 stackprotector
Solution 9 Jin Lee