I'm trying to code my first Spring application using Netbeans IDE. My application directory structure in the netbeans ide view is as follows.
MySpringApp1--Source Packages--<default package>--newSpringXMLConfig.xml
When I create a BeanFactory instance in MySpringApp1.java by coding as follows.
I get the following exception.
Feb 11, 2013 10:07:39 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from file [C:\Users\SomeUser\Documents\NetBeansProjects\MySpringApp1\newSpringXMLConfig.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [C:\Users\SomeUser\Documents\NetBeansProjects\MySpringApp1\newSpringXMLConfig.xml]; nested exception is java.io.FileNotFoundException: newSpringXMLConfig.xml (The system cannot find the file specified)
Caused by: java.io.FileNotFoundException: newSpringXMLConfig.xml (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
... 4 more
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
But when I create an ApplicationContext instance by coding as follows,
my code runs fine.
Could somebody please help me resolve the FileNotFoundException I'm getting in the BeanFactory instance case and also help me understand why I'm getting it. Since my xml configuration file is in the default package, why it shouldn't be found?
I have not set CLASSPATH explicitly by either setting it up through command line commands or through environment variables though I ain't sure this is a classpath problem ( or is it? ) cause it works when I create an ApplicationContext instance. Please help.
Chan Ag wrote:Could somebody please help me resolve the FileNotFoundException I'm getting in the BeanFactory instance case and also help me understand why I'm getting it. Since my xml configuration file is in the default package, why it shouldn't be found?
It shouldn't be found because the FileSystemResource class looks in (not surprisingly) the file system to get the file you asked for. And when you supply a relative path, like you did, it looks in the current working directory. That's how the file system works. It knows nothing about packages and it knows nothing about the classpath.
So if you want to use FileSystemResource, you have to provide the full path to the file, since you won't know what the current working directory is going to be. On the other hand if you know that your resource is in the classpath, then you should use something which searches the classpath instead. Which you did. And it worked.
Joined: Sep 06, 2012
Thanks for your response.
I changed the code as follows
and it worked :-)
And like you said, the following worked also.
Thank you so much.
subject: java.io.FileNotFoundException when trying to create BeanFactory instance using FileSystemResource