6

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [com/main/beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [com/main/beans.xml] cannot be opened because it does not exist

ApplicationContext context = 
      new ClassPathXmlApplicationContext("com/main/beans.xml");

I have tried before with

ApplicationContext context = 
     new FileSystemXmlApplicationContext("src/main/java/com/main/beans.xml");

And it works well.

How to do that relative to the classpath?

Note: classpath is in the build path


In the example I'm following, it has the following structure and it works

Project structure

Project structure

Classpath

Classpath

ApplicationContext context = 
    new ClassPathXmlApplicationContext("com/caveofprogramming/spring/test/beans/beans.xml");
Derrick
  • 3,669
  • 5
  • 35
  • 50
Joe
  • 7,749
  • 19
  • 60
  • 110
  • 4
    xml files should be in `src/main/resources` not in java. When compiling with maven the xml files are ignored if they aren't in resources (Unless you do some extra work to add them back). – M. Deinum Jan 31 '14 at 11:50
  • see my **UPDATE** below – Paul Samsotha Jan 31 '14 at 12:34
  • @M.Deinum thank you -- useful! But why it worked in the example? Seems that author didn't do some extra work – raiym Sep 09 '15 at 12:33
  • It might work from within eclipse (as that sucks at computing the classpath especially with Maven projects), when building the jar/war it won't be in that location. – M. Deinum Sep 09 '15 at 12:37
  • Keep you beans.xml in classpath or src/main/...pkg where java file are saved. And then use ClassPathXmlApplicationCOntext – Ashish Ani Dec 26 '15 at 06:19
  • Please see my answer at : https://stackoverflow.com/a/66230703/5916535. Hope it helps – Nafeez Quraishi Feb 16 '21 at 19:14

1 Answers1

6

Here's the file structure I normally use, which works fine. As @M.Deinum said, you'll want to put your xml file in a src/main/resources. I normally put to the it in a complete package path with the resources so during compile time, maven will add all the resources to same path as the corresponding classes that use them.

enter image description here

resources get copied to the class package when you do the above

enter image description here

public class App {

    public static void main(String[] args) {
        ApplicationContext context
                = new ClassPathXmlApplicationContext("com/underdogdevs/stackmaven/beans.xml");

        Hello hello = (Hello) context.getBean("hello");
        hello.sayHello();
    }
}

Works fine for me. If you're wondering why you still need to use the complete package name when the xml is already in the same class packages, its it will first be searched for in the class root


UPDATE

put the package with the bean.xml into the src/main/resources. It should work with the path your using.


UPDATE 2

"Yes, it worked. But why is it working the example, I'm following as well. If the beans.xml is out of src/main/resources .. I can't find out how that works? *

The thing is, the Spring container will look from the class root. It has nothing to with the resources folder. The resources is a convenience dir for maven projects to build to your class path. The reason the tutorial works, is that the beans.xml is in a package, that will get put into the class path in the build, as seen below. It is only preferred to use a resources, but a package` will also build to the class path.

enter image description here enter image description here

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Yes, it worked. But why is it working the example, I'm following as well. If the beans.xml is out of src/main/resources http://173.248.174.172/~caveofpr/wp-content/uploads/2013/10/spring-tutorial-8.zip I can't find out how that works? – Joe Jan 31 '14 at 13:10
  • See my **UPDATE** 2. Hope that clarifies things. – Paul Samsotha Jan 31 '14 at 13:41
  • Yes, clear me up this. I also say that in Build path I had *.java instead of all, that the example had. – Joe Jan 31 '14 at 14:55
  • Now I tried to run but it come up with another problem, eclipse now tell me that 'Selection does not have main type'. Is my eclipse proyect corrupt? – Joe Jan 31 '14 at 14:56
  • See [**here**](https://www.google.com/search?q=Selection%20does%20not%20have%20main%20type%27). It could be a couple different things. Also make sure the class with your `main` method is the Main Class. You can do to your run config to see that. – Paul Samsotha Jan 31 '14 at 15:14