1

I am having problem trying to run my first spring application that displays Hello world at my console. I am getting this error.

Oct 16, 2013 10:24:37 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5d764be1: startup date [Wed Oct 16 10:24:37 EAT 2013]; root of context hierarchy
Oct 16, 2013 10:24:37 AM 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:341)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:527)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:441)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at come.tutorialspoint.MainApp.main(MainApp.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:157)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
    ... 13 more

here is the Beans.xml file:

<?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-
3.0.xsd">
<bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
<property name="message" value="Hello World!"/>
 </bean>
</beans>

I have create the Beans.xml file in the exact package by right clicking and adding android .xml file. Here is the java class:

package come.tutorialspoint;

public class HelloWorld {
   private String message;

   public void setMessage(String message){
      this.message  = message;
   }

   public void getMessage(){
      System.out.println("Your Message : " + message);
   }
}

Here is the Main class:

public class MainApp {
public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
    HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
    obj.getMessage();
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
link
  • 55
  • 1
  • 8
  • Well it's failing to find Beans.xml... so do you have that file, and where is it? – Jon Skeet Oct 16 '13 at 07:46
  • Please attach here the code that starts the application context and your project structure (the folders and where each file resides) – Avi Oct 16 '13 at 07:47
  • @JonSkeet I have the Beans.xml file at my package. I will post the exact code. – link Oct 16 '13 at 07:49
  • @Avi I have attached the code – link Oct 16 '13 at 08:09
  • @link - Please publish the project structure - the folders you have and where does each file reside. – Avi Oct 16 '13 at 08:12
  • @Avi I don't understand, 'project structure', do you mean my workspace and project files or the place i put my jar files? – link Oct 16 '13 at 08:15
  • Your source code resides in folders. For example - something like `src/main/java/com/mycompany/utils/HelloWorld.java` (for a file `HelloWorld.java` in package `com.mycompany.utils`) I need to understand where your `beans.xml` is in your projects structure. – Avi Oct 16 '13 at 08:20
  • The reason I'm asking for it is because you're getting a `FileNotFoundException` which means that your `beans.xml` file is not in the root of your class path (which is where you're trying to load it from), it somewhere else and I need to understand where. – Avi Oct 16 '13 at 08:21
  • Ok, The java class /Springfirst/src/come/tutorialspoint/HelloWorld.java, the .xml: /Springfirst/src/come/tutorialspoint/Beans.xml the main app class : /Springfirst/src/come/tutorialspoint/MainApp.java The Springfirst being the project name and tutorialspoint is the package name – link Oct 16 '13 at 08:23
  • /home/aaaa/Documents/workspace.aaaa/Springfirst/src/come/tutorialspoint/MainApp.java, /home/aaaa/Documents/workspace.aaaa/Springfirst/src/come/tutorialspoint/Beans.xml /home/aaaa/Documents/workspace.aaaa/Springfirst/src/come/tutorialspoint/HelloWorld.java @Avi – link Oct 16 '13 at 08:41
  • 1
    Try to have a look at [this stackoverflow topic](http://stackoverflow.com/questions/12893760/spring-cannot-find-bean-xml-configuration-file-when-it-does-exist). Maybe it will be useful. – Paolo Oct 16 '13 at 08:50

2 Answers2

0

Based on one of your last comments to the question, your Beans.xml is in the wrong location. Options:

  • Move Beans.xml to "src/main/resources/Beans.xml"

or

  • Move Beans.xml to "src/main/java/Beans.xml"

or

  • Change ClassPathXmlApplicationContext("Beans.xml") to ClassPathXmlApplicationContext("come/tutorialspoint/Beans.xml");

The first option is the preferred option per Java/Maven conventions.

Your error is that ClassPathXmlApplicationContext expects a classpath location, and that includes any package information. Your Beans.xml is currently sitting in the package "come.tutorialspoint";

For more info:

Community
  • 1
  • 1
kaliatech
  • 17,579
  • 5
  • 72
  • 84
  • Just noticed I answered a question that's almost eight months old since last activity. Not sure why it was in the recent questions feed. – kaliatech Jun 25 '14 at 11:38
0

Keep you Bean.xml in same package of your Java files and instead Of using:

ClassPathXmlApplicationContext("Beans.xml")

Use:

ClassPathXmlApplicationContext("come/tutorialspoint/Beans.xml");
Bugs
  • 4,491
  • 9
  • 32
  • 41
varunj
  • 13
  • 3