java.io.FileNotFoundException:类路径资源 [英] java.io.FileNotFoundException: class path resource

查看:64
本文介绍了java.io.FileNotFoundException:类路径资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

推荐答案

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

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.

>

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

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

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

这篇关于java.io.FileNotFoundException:类路径资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆