• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.












 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic