aspose file tools*
The moose likes Spring and the fly likes [beans.xml] cannot be opened because it does not exist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Reply locked New topic
Author

[beans.xml] cannot be opened because it does not exist

Shanmugam Muthukumarasamy
Ranch Hand

Joined: Jan 11, 2003
Posts: 83
Hi,

Though I have added the beans.xml into the classpath of my project, I am getting the FileNotFoundException on the beans configuration file. Any help on this really appreciated.

Dec 21, 2008 10:28:45 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@ab50cd: display name [org.springframework.context.support.ClassPathXmlApplicationContext@ab50cd]; startup date [Sun Dec 21 22:28:45 PST 2008]; root of context hierarchy
Dec 21, 2008 10:28:45 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:349)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:113)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:80)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:123)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:422)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.apress.springrecipes.hello.Main.main(Main.java:10)
Caused by: java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:143)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
... 13 more


SJCP 1.4<br />SCWCD 1.4<br />SCEA
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Where do you run this application from ? From the command line or from an IDE ?


[My Blog]
All roads lead to JavaRanch
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Either way, where is your file. What directory. Based on the error, it is looking for the file on the ROOT of your CLASSPATH. If the file is in some other "directory" then it won't be found.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Shanmugam Muthukumarasamy
Ranch Hand

Joined: Jan 11, 2003
Posts: 83
I am using the Eclipse IDE.

1. My workspace location
C:\MY-WORKSHOP\wsSpring

2. Project structure
com.apress.springrecipes.hello.Main.main
(a) C:\MY-WORKSHOP\wsSpring\HelloWorld\src\com\apress\springrecipes\hello\Main.java
(b) C:\MY-WORKSHOP\wsSpring\HelloWorld\beans.xml

Main object loads the beans.xml from main() method like below:

ApplicationContext context =
new ClassPathXMLApplicationContext("beans.xml");

3. Also this beans.xml has been added to the classpath of HelloWorld (Spring) project using "Add Jars".

When I run the Main class it throws the above stated exceptions.

I am new to Spring, your help on this really appreciated.
[ December 22, 2008: Message edited by: Shanmugam Muthukumarasamy ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Yeah, you can't add it to the classpath with "add jar", just move the xml file up one more directory to be completely at the root of your classpath. So put it under "src" so it looks like below.

C:\MY-WORKSHOP\wsSpring\HelloWorld\src\beans.xml


Mark
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Or in Eclipse, editing your execution properties to make the root of the project in the classpath.
Shanmugam Muthukumarasamy
Ranch Hand

Joined: Jan 11, 2003
Posts: 83
Thank you for your responses!

Christophe Verre,
Just curious, how do you edit the execution properties in Eclipse?

Thanks,
Shan
Shanmugam Muthukumarasamy
Ranch Hand

Joined: Jan 11, 2003
Posts: 83
Mark Spritzler,

Another interesting point that I noticed while creating the beans.xml.

I have followed the steps below to create the beans.xml

1. Right click on my project (HelloWorld) and "New" -> "Spring" -> "Create Bean Definition".
2. This creates the beans.xml right under the HelloWorld folder.

That is the reason I kept that file under the same location. I will try your suggestion and let you know.

Shan
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Originally posted by Shanmugam Muthukumarasamy:
Mark Spritzler,

Another interesting point that I noticed while creating the beans.xml.

I have followed the steps below to create the beans.xml

1. Right click on my project (HelloWorld) and "New" -> "Spring" -> "Create Bean Definition".
2. This creates the beans.xml right under the HelloWorld folder.

That is the reason I kept that file under the same location. I will try your suggestion and let you know.

Shan


Actually that isn't quite true.

You put it there because that is where your cursor was pointing over when you right clicked. If you select the src and have your mouse over that directory and right-click and select the Create Bean Definition, then it will put the xml file in the src directory.

Mark
Shanmugam Muthukumarasamy
Ranch Hand

Joined: Jan 11, 2003
Posts: 83
Thank you Mark! I will try that.
swati gudla
Greenhorn

Joined: Aug 08, 2008
Posts: 6
Hello Everyone,
I am very new to spring framework.
I have similar issue.
I tried to move my bean.xml file to Src .But it did not work and it gives me back the same error.
And about my bean.xml file it looks some thing like this
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="helloWord" class="com.apress.springrecipes.hello.HelloWord">
<property name="message" value="How are you?" />
</bean>
</beans>
I dont know what is wrong previously my beans.xml file was there inside the project based on the suggestion on this page i moved to src but no good end result.

I would really appreciate if some one can answer my question...
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I dont know what is wrong previously my beans.xml file was there inside the project based on the suggestion on this page i moved to src but no good end result.

This thread is not suggesting to put the xml file in an src directory. First, show us how you are trying to access the xml file. For example, the original poster is using the following :

What are you using ?
swati gudla
Greenhorn

Joined: Aug 08, 2008
Posts: 6
Hello
Thanks for your quick reply...
i have bean called Helloworld
which has private variable called message and getter and setter method.
My bean.xml file loos like

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="helloWorld" class="com.apress.springrecipes.hello.HelloWorld">
<property name="message" value="How are you?" />
</bean>
</beans>


And i tried putting my beans.xml file in src folder as well as in the project folder nothign worked for me


And i accessing the beans.xml file in the following way
ApplicationContext context =new ClassPathXmlApplicationContext("complete file path of beans.xml");
HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");
helloWorld.hello();

Which throws exception

INFO: Loading XML bean definitions from class path resource [beans.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:349)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:113)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:80)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:123)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:422)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.apress.springrecipes.hello.Main.main(Main.java:10)
Caused by: java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:143)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)

The same exception what this thread is discussign about..
I would really appreciate if some oen can answer me to this question.

Thanks,
swati
Trilochan Bharadwaj
Ranch Hand

Joined: Feb 02, 2009
Posts: 100
Dude,

Use FileSystemXXXAppContext and Resource if you wanna use the full file path, but if you still wanna use the xml file, put it under a package within a source folder, something like say src/main/resources (<-- this would be your source folder), then use a package x.y.y and put the file in there, then use x/y/z/beans.xml in your app context initialization code.

Also, if you don't wanna package your stuff like that, put the xml inside the package where your "Hello World" POJO lies.



Trilochan.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

ClassPathXmlApplicationContext allows you to search for context files in the CLASSPATH. I assume that you know what the classpath is. If you don't, check this FAQ. If you want to use a full path, use FileSystemXmlApplicationContext instead (echoing Trilochan's suggestion).
muthukumaran kanagasabapathy
Greenhorn

Joined: Sep 30, 2009
Posts: 2
Christophe Verré wrote:ClassPathXmlApplicationContext allows you to search for context files in the CLASSPATH. I assume that you know what the classpath is. If you don't, check this FAQ. If you want to use a full path, use FileSystemXmlApplicationContext instead (echoing Trilochan's suggestion).



i am getting the same error


run:
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [context.xml]; nested exception is java.io.FileNotFoundException: class path resource [context.xml] cannot be opened because it does not exist
java.io.FileNotFoundException: class path resource [context.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:137)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:167)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:148)
at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:73)
at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:61)
at xmlbeaninjection.Main.main(Main.java:22)
BUILD SUCCESSFUL (total time: 0 seconds)
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Trilochan Bharadwaj wrote:Dude,

Use FileSystemXXXAppContext and Resource if you wanna use the full file path, but if you still wanna use the xml file, put it under a package within a source folder, something like say src/main/resources (<-- this would be your source folder), then use a package x.y.y and put the file in there, then use x/y/z/beans.xml in your app context initialization code.

Also, if you don't wanna package your stuff like that, put the xml inside the package where your "Hello World" POJO lies.

Trilochan.

Thank you, Trilochan. There was no feedback on your advice so I want to thank you and tell you that it worked for me. By coincidence, I was struggling with the exact same code as Shanmugam's (taken from the book, "Spring 2.5 Recipes"). I had made every manual change to classpaths that I could think of in order to get the beans.xml configuration file to be accessible by the Java code but failed until I took your advice. After moving the XML file into the code package, I changed the context object's instantiation code (line 11) to specify the complete path to the XML file:




SCJP 6
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

I doubt, he will see the thanks. This is almost a year old thread.

So I am going to close this thread.

Thanks

Mark

p.s. also note, with any of the ApplicationContext objects, you can always just add a prefix to change the lookup type. So if you use

ClassPathXmlApplicationContext(new String[]{"com/mark/beans.xml","file:C:\mylocation\beans2.xml", "http:WEB-INF\beans3.xml"});

The first file is looked for in the classpath, the second at a file system location, and the third in the Web environment.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [beans.xml] cannot be opened because it does not exist
 
Similar Threads
Problem in code
Quartz in spring
applicationContext.xml locating problem
Main Exception
FileNotFoundException for context file