1

In Spring framework for Java, I create my beans using context.xml file and then load it with ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");.

My program throws an exception java.io.FileNotFoundException: class path resource [context.xml] cannot be opened because it does not exist.

The file exists. I use Gradle for dependencies management and my src folder (where the file is) is marked as resource in Intellij. I understand that for some reason even though my src folder should be included in classpath variable, gradle run task just can't grab it from here.

When I use new FileSystemXmlApplicationContext("context.xml"); instead everything works just fine, but I do need to use "Classpath" for my assignment.

English is not my native language.

This is my "Main" class:

public class Main { public static void main(String[] args) { ApplicationContext context = new FileSystemXmlApplicationContext("context.xml"); } } 

This is my beans from context.xml file

<bean id="t1000" class="by.zti.main.impl.T1000"> <constructor-arg ref="sonyHead"/> <constructor-arg ref="toshibaLeg"/> </bean> <bean id="sonyHead" class="by.zti.main.impl.SonyHead"/> <bean id="sonyLeg" class="by.zti.main.impl.SonyLeg"/> <bean id="toshibaHead" class="by.zti.main.impl.ToshibaHead"/> <bean id="toshibaLeg" class="by.zti.main.impl.ToshibaLeg"/> 

This is my build.gradle file

group 'zti.spring.test' version '1.0-SNAPSHOT' apply plugin: 'java' apply plugin: 'application' apply plugin: 'idea' sourceCompatibility = 1.8 targetCompatibility = 1.8 mainClassName = 'by.zti.main.Main' repositories { mavenCentral() } jar { baseName = 'gs-gradle' version = '0.1.0' } dependencies { compile 'org.springframework:spring-context:4.3.6.RELEASE' compile fileTree(dir:'src', includes: ['/*.xml,/*.properties']) } 

Exception text

 Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [context.xml]; nested exception is java.io.FileNotFoundException: class path resource [context.xml] cannot be opened because it does not exist at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252) 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:129) at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:613) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:514) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83) at by.zti.main.Main.main(Main.java:12) Caused by: java.io.FileNotFoundException: class path resource [context.xml] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330) ... 13 more 

This is my file tree from my project

enter image description here

Content of my classpath

/C:/Users/Cvazer/IdeaProjects/spring-test/build/classes/main/ /C:/Users/Cvazer/IdeaProjects/spring-test/build/resources/main /C:/Users/Cvazer/.gradle/caches/modules-2/files-2.1/org.springframework/spring-context/4.3.6.RELEASE/5f3041020113e3202a7ae9defe36e7b5d2fb87eb/spring-context-4.3.6.RELEASE.jar /C:/Users/Cvazer/.gradle/caches/modules-2/files-2.1/org.springframework/spring-aop/4.3.6.RELEASE/d70b2393d380cd468d9a14969efaf9022c67c0/spring-aop-4.3.6.RELEASE.jar /C:/Users/Cvazer/.gradle/caches/modules-2/files-2.1/org.springframework/spring-beans/4.3.6.RELEASE/1585715ed598b76a23dac931c27aa039b189eafb/spring-beans-4.3.6.RELEASE.jar /C:/Users/Cvazer/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/4.3.6.RELEASE/690da099c3c2d2536210f0fd06ff3f336de43ad9/spring-core-4.3.6.RELEASE.jar /C:/Users/Cvazer/.gradle/caches/modules-2/files-2.1/org.springframework/spring-expression/4.3.6.RELEASE/13b53568cfd7b308e70efcbac6cdd0c5d597ba1/spring-expression-4.3.6.RELEASE.jar /C:/Users/Cvazer/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar 
4
  • This link could help u stackoverflow.com/questions/10412225/… Commented Feb 20, 2017 at 14:11
  • I think this is classpath issue. Could you put the context.xml in src/main/resources folder. If you prefer existing folder structue then make sure the path is recognized by classpath Commented Feb 20, 2017 at 14:23
  • "src folder is marked as resource" - there's the problem, the resources folder should be "src/main/resources" Commented Feb 20, 2017 at 17:16
  • Possible duplicate of Spring cannot find bean xml configuration file when it does exist Commented Feb 21, 2017 at 23:12

2 Answers 2

0

I thin you should put the file in src/main/resources/ OR specify another path ClassPathXmlApplicationContext("src/main/resources/blablabla/context.xml")

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

5 Comments

src should not be on the classpath, your alternate example is incorrect
Yes, those answers are bad too.
Those answers are marked as useful, I don't think all people who voted up are wrong... There are probably many seniors who have seen that...
They're upvoted because they made the code work. They can still be wrong. The src/main/resources directory is a source directory, it's not supposed to be on the classpath and you're not supposed to load resources from there.
0

you should append "file:" ie your url should be like this "file:src/main/resources/filename.xml"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.