javanoob
javanoob

Reputation: 6410

FileNotFoundException..Classpath resource not found in spring?

I have code like this in Main.java :

AbstractApplicationContext context  = new ClassPathXmlApplicationContext("spring-config.xml");

Until recently it was working, but I don't know why it started failing with the below exception:

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring-config.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring-config.xml] cannot be opened because it does not exist

the spring-config.xml is in src/main/resources folder.

Actually I wanted to learn about the annotations: @Postconstruct and @Predestroy, so I changed the build path to Jdk 1.6 from Jdk 1.5.

Since then the problem started...

Any clue why it is not working?

NOTE: If any wants to see my project structure please follow this link http://code.google.com/p/javapracticeram/source/browse/trunk/SpringExample/

EDIT: alt text

Upvotes: 8

Views: 107905

Answers (6)

Satya Tiwari
Satya Tiwari

Reputation: 103

Best way to handle such error-"Use Annotation". spring.xml-<context:component-scan base-package=com.SpringCollection.SpringCollection"/>

add annotation in that class for which you want to use Bean ID(i am using class "First")-

@Component

public class First {

Changes In Main Class**-

ApplicationContext context = new AnnotationConfigApplicationContext(First.class); use this.

Upvotes: -2

kaykae
kaykae

Reputation: 731

I was getting the same problem when running my project. On checking the files structure, I realised that Spring's xml file was inside the project's package and thus couldn't be found. I put it outside the package and it worked just fine.

Upvotes: 0

tolitius
tolitius

Reputation: 22509

Looking at your classpath you exclude src/main/resources and src/test/resources:

    <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
    <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>

Is there a reason for it? Try not to exclude a classpath to spring-config.xml :)

Upvotes: 12

Ryan Stewart
Ryan Stewart

Reputation: 128829

Two things worth pointing out:

  1. The scope of your spring-context dependency shouldn't be "runtime", but "compile", which is the default, so you can just remove the scope line.
  2. You should configure the compiler plugin to compile to at least java 1.5 to handle the annotations when building with Maven. (Can also affect IDE settings, though Eclipse doesn't tend to care.)

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    

After that, reconfiguring your project from Maven should fix it. I don't recall exactly how to do that in Eclipse, but you should find it if you right click the project node and poke around the menus.

Upvotes: 0

earldouglas
earldouglas

Reputation: 13473

Check the contents of SpringExample/target/classes. Is spring-config.xml there? If not, try manually removing the SpringExample/target/ directory, and force a rebuild with Project=>Clean... in Eclipse.

Upvotes: 3

YoK
YoK

Reputation: 14505

This is due to spring-config.xml is not in classpath.

Add complete path of spring-config.xml to your classpath.

Also write command you execute to run your project. You can check classpath in command.

Upvotes: 0

Related Questions