cbll
cbll

Reputation: 7219

Spring class path resource [applicationContext.xml] cannot be opened because it does not exist

I'm placing applicationContext.xml in the same directory and package as my Java classes.

Doing the following to read it:

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

Yet I am consistently met with:

IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException

Which I do not get: The file is there, in the same directory as the classes. They're even compiled to /out and I can see it's all there.

I've tried putting it in src/resources/applicationContext.xml but to no avail.

Upvotes: 3

Views: 7906

Answers (2)

DS_CodeGeek
DS_CodeGeek

Reputation: 11

I resolved this problem, as changing

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

to

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

My XML file also resides in the same package as my java classes exist.

Upvotes: 0

glytching
glytching

Reputation: 48005

Spring will look for it in the root of the classpath ...

  • The "same directory and package as my Java classes" is not at the root of your classpath
  • src/resources is not on your classpath at all

If you put it in src/main/resources then it will be (1) on your classpath and (2) in the root of your classpath.

Upvotes: 8

Related Questions