FileNotFound Exception trying a simple Spring java application

I have the following "Beans.xml"; it is in eclipse under 'src' folder:

    <?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="user1" class="rc.User">
            <property name="name"    value="joe" />
        </bean>

    </beans>

      

I have the following java application:

package rc;

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

public class Asparagus 
{
    public static void main (String[] arguments)
    {
        ApplicationContext context = null;
        try
        {
            context = new ClassPathXmlApplicationContext("Beans.xml");
        }
        catch (ExceptionInInitializerError eiie)
        {
            System.out.println(eiie);
        }
        System.out.println("hi");
        User u = context.getBean("user1", User.class);
        System.out.println("hi2");
        System.out.println(u);
    }
}

      

This is in the 'rc' package inside the 'src' folder in eclipse. I have a custom bean class, I think I could include it as well:

package rc;

public class User 
{
    public User() {} 

    public String name;

    public String getName()                {return name;            }
    public void setName(String name)       {this.name = name;       }
}

      

And when I run this, I get a FileNotFoundException. The only file I can think of would be trying to find my XML file, but I can't figure out why it must have problems.

I looked using DOS in the directory structure, checking that "Beans.xml" is at the root of the application path, in both src and bin. I think this is supported by the fact that the Asparagus class (someone objected to it being called "Main") is found and run, which eclipse cannot do if src / bin is not the root of the classpath.

I recently included a mess in the library on the recommendation of someone else and this problem went away and I got another one I found (on StackOverflow) caused by the jar being included in eclipse as a system library, so I pulled out all the system libraries excluding the java runtime.

I would REALLY prefer not to allow this, adding a lot of libraries without knowing anything about what they are supposed to do. Can someone please explain what is going on here and why the suggested solution might fix it?

+3


source to share


2 answers


This turned out to be some kind of vague problem with the spring jars used in the project.

I had a full set of spring jars - 8 or 10 of them - all named in the form org.springframework.-3.1.1.RELEASE.jar; they were placed as jars on the build path in the spring Tool Suite (aka eclipse). Another programmer had a similar simple project that worked with jars in spring form - 3.1.2.RELEASE.jar, so I switched to those. The code now works.



Thanks to everyone who offered help; I still don't know the difference between the two sets of jars, or why the original error message was unclear (any code that throws a FileNotFoundException must tell us something about which file).

0


source


ClassPa thanks mlApplicationContext: You don't need to specify the full path, but you need to set it CLASSPATH

correctly. If this is a code exception, it means the container did not find the bean definition in the classpath. Please check your classpath and try again.

If you are using FileSystemXmlApplicationContext , you need to provide the full XML bean path.



For reference Spring Container ApplicationContext

Spring 3.1.x supports annotation. So I preferred annotation @Autowired

to initialize any bean.

0


source







All Articles