Syed Siraj Uddin
Syed Siraj Uddin

Reputation: 593

Class path resource can not be opened

I am trying to load a file name SpringConfig.xml from directory

resources\cfg\SpringConfig.xml using the

context = new ClassPathXmlApplicationContext("/cfg/SpringConfig.xml") 

which does not work, but the same thing if I change the location as resources\SpringConfig.xml it does work fine with

context = new ClassPathXmlApplicationContext("SpringConfig.xml")

but I want to use the prior one, can someone help on how I fix this?

Upvotes: 0

Views: 401

Answers (3)

Jigar Joshi
Jigar Joshi

Reputation: 240996

Try with

context = new ClassPathXmlApplicationContext("cfg/SpringConfig.xml") 

Upvotes: 0

user647772
user647772

Reputation:

Use / instead of \ in your Strings.

Upvotes: 3

Simon Nickerson
Simon Nickerson

Reputation: 43177

Backslashes are special in Java. Try switching to forward slashes. (In fact, it shouldn't even compile with the backslashes in your example!)

Similarly, the initial slash should probably be omitted, as otherwise you're specifying an absolute rather than a relative path.

Upvotes: 1

Related Questions