java.io.FileNotFoundException:类路径资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33106591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:20:26  来源:igfitidea点击:

java.io.FileNotFoundException: class path resource

javaxmlspringspring-mvc

提问by abhishek shivdekar

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.*;

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

    Resource res= new ClassPathResource("E:/JAVA/Springs Netbean Projects/Employee/src/contructor/applicationContext.xml");
    BeanFactory factory=new XmlBeanFactory(res);

    Employee s=(Employee)factory.getBean("e");

    }
}

Above is my program and the error show is:

以上是我的程序,错误显示是:

Oct 13, 2015 8:42:28 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [E:/JAVA/Springs Netbean Projects/Employee/src/contructor/applicationContext.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [E:/JAVA/Springs Netbean Projects/Employee/src/contructor/applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [E:/JAVA/Springs Netbean Projects/Employee/src/contructor/applicationContext.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:341)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
    at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:73)
    at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:61)
    at contructor.Test.main(Test.java:21)
Caused by: java.io.FileNotFoundException: class path resource [E:/JAVA/Springs Netbean Projects/Employee/src/contructor/applicationContext.xml] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:141)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)

What could be the possible problem? please help as i am new to springs

可能的问题是什么?请帮忙,因为我是春天的新手

I am facing same problem, how can i solve it?

我面临同样的问题,我该如何解决?

采纳答案by Tunaki

You are trying to get a reference to a file as a classpath resource but the path you have given is not a path of a file inside the classpath.

您正在尝试获取对文件的引用作为类路径资源,但您提供的路径不是类路径中文件的路径。

Instead of harcoding the full path to the file, use a path that is relative to the root source of the project. If srcis the root source directory, use this:

不要对文件的完整路径进行硬编码,而是使用相对于项目根源的路径。如果src是根源目录,请使用:

Resource res = new ClassPathResource("/contructor/applicationContext.xml");

回答by Saravana

Because it is ClassPathResourceI think contructor/applicationContext.xmlis enough

因为是ClassPathResource我觉得contructor/applicationContext.xml就够了