Skip to content
Advertisement

Spring/NetBeans – java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not exist

Whenever I try to run my java project, I get file not found exception saying that beans.xml cannot be found. I am using NetBeans and have read that I might need to set my classpath to the correct directory or the project will not run correctly (Eclipse does this automatically(?)). Running this same program with same library works with Eclipse. I don’t know if my problem is Spring related or classpath related (but I assume it’s a classpath issue).

MainApp.java

package hello;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        HelloWorld hello = (HelloWorld) context.getBean("helloWorld");
                hello.getMessage();
    }
}

HelloWorld.java

package hello;

public class HelloWorld {
        private String message;

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

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

beans.xml

<?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 = "hello.HelloWorld">
      <property name = "message" value = "Hello World!"/>
   </bean>

</beans>

ST

run:
    Mar 20, 2017 11:05:04 PM     org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    INFO: Refreshing     org.springframework.context.support.ClassPathXmlApplicationContext@4ee285c    6: startup date [Mon Mar 20 23:05:04 EDT 2017]; root of context hierarchy
    Mar 20, 2017 11:05:04 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:344)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252)
        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:129)
        at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:613)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:514)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
        at hello.MainApp.main(MainApp.java:8)
    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:172)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)
        ... 13 more
    /home/john/.cache/netbeans/8.1/executor-snippets/run.xml:53: Java returned: 1
    BUILD FAILED (total time: 0 seconds)

File and Project structures

Thanks for the help.

SOLUTION: In NetBeans the beans.xml type file you are using to instantiate your beans must contain it’s path. In my case, I had to type out:

AbstractApplicationContext context = new ClassPathXmlApplicationContext("/hello/beans.xml");

Instead of just:

AbstractApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

Advertisement

Answer

Your beans.xml is in hello package so you should refer it using:

AbstractApplicationContext context = new ClassPathXmlApplicationContext("hello/beans.xml");

P.S.: A better practice would be to place this in resources directory.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement