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"); 
6
  • 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). Commented Jan 31, 2014 at 11:50
  • see my UPDATE below Commented Jan 31, 2014 at 12:34
  • @M.Deinum thank you -- useful! But why it worked in the example? Seems that author didn't do some extra work Commented Sep 9, 2015 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. Commented Sep 9, 2015 at 12:37
  • Keep you beans.xml in classpath or src/main/...pkg where java file are saved. And then use ClassPathXmlApplicationCOntext Commented Dec 26, 2015 at 6:19

1 Answer 1

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 you're 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

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

5 Comments

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 173.248.174.172/~caveofpr/wp-content/uploads/2013/10/… I can't find out how that works?
See my UPDATE 2. Hope that clarifies things.
Yes, clear me up this. I also say that in Build path I had *.java instead of all, that the example had.
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?
See here. 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.