File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes java.io.FileNotFoundException when trying to create BeanFactory instance using FileSystemResource Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "java.io.FileNotFoundException when trying to create BeanFactory instance using FileSystemResource" Watch "java.io.FileNotFoundException when trying to create BeanFactory instance using FileSystemResource" New topic
Author

java.io.FileNotFoundException when trying to create BeanFactory instance using FileSystemResource

Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1049
    
  15
Hi,

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
MySpringApp1--Source Packages--myspringapp1--HelloWorld.java
MySpringApp1--Source Packages--myspringapp1--MySpringApp1.java

When I create a BeanFactory instance in MySpringApp1.java by coding as follows.

I get the following exception.

run:
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)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:73)
at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:61)
at myspringapp1.MySpringApp1.main(MySpringApp1.java:25)
Caused by: java.io.FileNotFoundException: newSpringXMLConfig.xml (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at org.springframework.core.io.FileSystemResource.getInputStream(FileSystemResource.java:110)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
... 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.

Thanks so much.












Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

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.
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1049
    
  15
Hi Paul,

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.

Chan.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.io.FileNotFoundException when trying to create BeanFactory instance using FileSystemResource