21

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?

2
  • 1
    isn't it usually of this pattern "src/main/resources/combined2.xml" ? Commented May 9, 2011 at 16:52
  • Please see my answer at : stackoverflow.com/a/66230703/5916535 Commented Feb 16, 2021 at 19:13

14 Answers 14

51

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.

Sign up to request clarification or add additional context in comments.

1 Comment

sir I am getting the same error but I am using java project instead of maven.
9

Maven, has standard directory for resources. Which is src/main/resources, so if you keep your file here it will take it.

For example

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

I had the same problem it worked for me

Comments

2

2023/4

if the classpath for resources is ok but still annoys, at a very least this works in Eclipse

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:combined2.xml");

(refer to Spring docs->Core paragraph ClassPathResource in Resources-> Built-in Resource Implementations)

Comments

1

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

Comments

1

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

Example:

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

Comments

0

Try this

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

Comments

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.

Comments

0

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.

Comments

0

Actually there are multiple solution for it, your choose whichever you want to implement in your project.

#1stStep
1) Move "combined2.xml" file to [src/main/resource]. 
2) Use *ApplicationContext context = new ApplicationContext(parent 
   class,args);*

*The [parent class] value is actually the main java class file or the current class where you are facing this issue.

#2ndStep
If you still wanna use your method, then consider following the solution 
below:-
1) Move "combined2.xml" file to [src/main/java]
2) Use *ClassPathXmlApplicationContext context = new 
   ClassPathXmlApplicationContext("combined2.xml");*

Elaboration of #2ndStep:- You put the xml file under wrong folder [src/main/java/resources], your "combined.xml" file should be under [src/main/java]. You don't have to give full directory of the path for an example: "src/main/java/resources/combined2.xml", just put the name of the [.xml] file you have created. Based on your project, you created "combined2.xml", just pass the name of the file which is "combined2.xml". The exact solution was given below based on your project structure.

Exact Solution:-

public RepeatingGrpPoC() {
ClassPathXmlApplicationContext appContext = new 
ClassPathXmlApplicationContext("combined2.xml");
}

Comments

0

This answer is for the ones that created project using start.spring.io.

  1. Remove @SpringBootApplication from your main class.
  2. Put yourXMLFile.xml in your resources folder.

Comments

0

Create the XML configuration file:

  1. Right-click on the src/main/resources directory.
  2. Select New -> File.
  3. Name the file config.xml.

[Create the config.xml File] : https://i.sstatic.net/LRGDEwfd.png

Comments

0

I also got the same problem. I put my spring.xml file in src/main/java because my class files are present in this directory. It solved my problem. You just have to put your xml file in class path that means where your classes are present. And also write the below code

ApplicationContext factory = new ClassPathXmlApplicationContext("spring.xml");

Comments

-1

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

enter image description here

Comments

-3

I got it.. correct answer

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

1 Comment

Dr Subhash Mohandas , the purpose of this post is to say thanks for the several existing answers which basically propose the same solution, isn't it? If you intend to contribute additional insight please make that more obvious by editing to explain the relevant difference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.