This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
    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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

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

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
Where do you run this application from ? From the command line or from an IDE ?
 
ranger
Posts: 17344
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
Shanmugam Muthukumarasamy
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 17344
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
Or in Eclipse, editing your execution properties to make the root of the project in the classpath.
 
Shanmugam Muthukumarasamy
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thank you for your responses!

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

Thanks,
Shan
 
Shanmugam Muthukumarasamy
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 17344
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Report post to moderator

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
Posts: 83
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thank you Mark! I will try that.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator

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
Posts: 6
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
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).
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Report post to moderator

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)
 
Ranch Hand
Posts: 247
  • Likes 1
  • Mark post as helpful
  • send pies
  • Report post to moderator

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:



 
Mark Spritzler
ranger
Posts: 17344
11
Mac IntelliJ IDE Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
What are you saying? I thought you said that Santa gave you that. And this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!