khaled
khaled

Reputation: 31

java spring framework code doesn't run

I have this code

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;*/
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DrawingApp {
public static void main(String[] args) {


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

    Triangle triangle = (Triangle) context.getBean("triangle");


 triangle.draw();
   }
 }

and I got this error

Jan 17, 2017 11:14:48 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6576fe71: startup date [Tue Jan 17 23:14:48 EST 2017]; root of context hierarchy Jan 17, 2017 11:14:48 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [spring.xml] Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring.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:522) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:436) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83) at fahad.DrawingApp.main(DrawingApp.java:14) Caused by: java.io.FileNotFoundException: class path resource [spring.xml] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328) ... 13 more


by the way, the spring.xml file and .classpath are at the same folder,

so what should I have to do to run this code ?

Thanks

Upvotes: 3

Views: 1330

Answers (2)

Srikanth Balaji
Srikanth Balaji

Reputation: 2718

To clearer picture, Please refer here - Class Path - Documentation, Java

The class path is the path that the Java runtime environment searches for classes and other resource files

The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.

Also, Please refer ClassPathXmlApplicationContext. If you place inside src folder, it would be referred.

Need a tutorial???, try the source code here ClassPathXmlApplicationContext-Tutorial- it uses ClassPathXmlApplicationContext.

Upvotes: 0

Nathan Hughes
Nathan Hughes

Reputation: 96385

It has nothing to do with the .classpath file, spring is expecting to find the spring.xml file in the classpath, meaning in the directories where your class files are stored.

If you follow the maven conventions for laying out your project then you would put the spring.xml under src/main/resources, you can specify it as a source folder in eclipse. At any rate put the file in a source folder with your code. The ide will copy any files it finds there into your classpath.

Upvotes: 3

Related Questions