3

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

7
  • Your spring.xml isn't on the runtime classpath. That said, use Spring Boot and Spring Initializr, which will autogenerate a fully booting application for you. Commented Jan 18, 2017 at 4:27
  • spring.xml should be inside resources folder if you are using maven Commented Jan 18, 2017 at 4:29
  • It clearly says - java.io.FileNotFoundException: class path resource [spring.xml]. Add a screenshot of your project structure. Commented Jan 18, 2017 at 4:29
  • what do you mean by project structure @SrikanthA Commented Jan 18, 2017 at 4:39
  • I'm not using maven, and the spring file is in the project folder @kuhajeyan Commented Jan 18, 2017 at 4:40

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

so you mean that Drawingapp.java and Triangle. java and spring.xml should be at the same folder which is src/mypackage/
@khaled: Put spring xml directly under src, not in mypackage.
thanks it works, I have another question sir, how can I know that I have JIT or not, because I heard that it comes with JVM, I have java 8 update 91, am I fine ?
0

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.

1 Comment

thanks it works, I have another question sir, how can I know that I have jit or not, because I heard that it comes with JVM, I have java 8 update 91, am I fine ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.