Spring cannot find bean xml configuration file when it does exist

256,876

Solution 1

Thanks, but that was not the solution. I found it out why it wasn't working for me.

Since I'd done a declaration:

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

I thought I would refer to root directory of the project when beans.xml file was there. Then I put the configuration file to src/main/resources and changed initialization to:

ApplicationContext context = new ClassPathXmlApplicationContext("src/main/resources/beans.xml");

it still was an IO Exception.

Then the file was left in src/main/resources/ but I changed declaration to:

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

and it solved the problem - maybe it will be helpful for someone.

thanks and cheers!

Edit:

Since I get many people thumbs up for the solution and had had first experience with Spring as student few years ago, I feel desire to explain shortly why it works.

When the project is being compiled and packaged, all the files and subdirs from 'src/main/java' in the project goes to the root directory of the packaged jar (the artifact we want to create). The same rule applies to 'src/main/resources'.

This is a convention respected by many tools like maven or sbt in process of building project (note: as a default configuration!). When code (from the post) was in running mode, it couldn't find nothing like "src/main/resources/beans.xml" due to the fact, that beans.xml was in the root of jar (copied to /beans.xml in created jar/ear/war).

When using ClassPathXmlApplicationContext, the proper location declaration for beans xml definitions, in this case, was "/beans.xml", since this is path where it belongs in jar and later on in classpath.

It can be verified by unpacking a jar with an archiver (i.e. rar) and see its content with the directories structure.

I would recommend reading articles about classpath as supplementary.

Solution 2

Try this:

new ClassPathXmlApplicationContext("file:src/main/resources/beans.xml");

file: preffix point to file system resources, not classpath.

file path can be relative or system (/home/user/Work/src...)

Solution 3

I also had a similar problem but because of a bit different cause so sharing here in case it can help anybody.

My file location

beans.xml file

How I was using

ClassPathXmlApplicationContext("beans.xml");

There are two solutions

  1. Take the beans.xml out of package and put in default package.
  2. Specify package name while using it viz.

ClassPathXmlApplicationContext("com/mypackage/beans.xml");

Solution 4

This is because applicationContect.xml or any_filename.XML is not placed under proper path.

Trouble shooting Steps

1: Add the XML file under the resource folder.

2: If you don't have a resource folder. Create one by navigating new by Right click on the project new > Source Folder, name it as resource and place your XML file under it.

Solution 5

src/main/resources is a source directory, you should not be referencing it directly. When you build/package the project the contents will be copied into the correct place for your classpath. You should then load it like this

new ClassPathXmlApplicationContext("beans.xml")

Or like this

new GenericXmlApplicationContext("classpath:beans.xml");
Share:
256,876

Related videos on Youtube

dawrutowicz
Author by

dawrutowicz

Updated on July 08, 2022

Comments

  • dawrutowicz
    dawrutowicz almost 2 years

    I am trying to make my first bean in Spring but got a problem with loading a context. I have a configuration XML file of the bean in src/main/resources.

    I receive the following IOException:

    Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [src/main/resources/beans.xml]; nested exception is

    java.io.FileNotFoundException: class path resource [src/main/resources/beans.xml] cannot be opened because it does not exist

    but I don't get it, since I do the following code test:

    File f = new File("src/main/resources/beans.xml");
    System.out.println("Exist test: " + f.exists());
    

    which gives me true! resources is in the classpath. What's wrong?

    • Aleksandr M
      Aleksandr M over 11 years
      How do you load context?
  • dawrutowicz
    dawrutowicz over 10 years
    well, it is better to keep resources in classpath than hard coded it but thanks, though. I didn't have experienced with CP that time :-)
  • dawrutowicz
    dawrutowicz about 9 years
    well, true to be said. Reference "com/mypackage/beans.xml" is same as "/com/mypackage/beans.xml" since "com/mypackage/beans.xml" is relevant path which uses "/" (root) to build absolute path.
  • romnempire
    romnempire over 8 years
    It's probably better to be using FileSystemXmlApplicationContext for files.
  • Florian F
    Florian F over 7 years
    I fail to see the difference between ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); and ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
  • dawrutowicz
    dawrutowicz over 7 years
    @FlorianF there is no difference. The difference is where the file is put, what is described in the post.
  • Florian F
    Florian F over 7 years
    I see you change beans.xml to src/main/resources/beans.xml and then back to beans.xml. Thanks.
  • Avdhut
    Avdhut over 7 years
    i has similar problem and this solution worked for me.
  • OrangeDog
    OrangeDog about 7 years
    You're not supposed to have src on the classpath. When you build the project those resources will be packaged somewhere else.
  • sailaja
    sailaja about 7 years
    Hi, this worked for me too but the problem is when the jar file is build the jar is not running for me..I mean, I am getting the NoClassDefFoundError while I try to run the jar file, when I run the application in eclipse as Java it's running without errors, not sure how to resolve this
  • Dhruv Singhal
    Dhruv Singhal almost 6 years
    Hi... Sorry for asking a bit late, but can you please elaborate on how to convert from input streamto application context? I am currently trying to run an executable jar file, but it says it cannot load the bean xml as it does not exist.
  • Dhruv Singhal
    Dhruv Singhal almost 6 years
    Hi, if we are running this jar from command line, it does not seem to be working as expected (working fine on eclipse).
  • Supun Sandaruwan
    Supun Sandaruwan about 2 years
    this should be the answer