无法打开classpath资源[spring / config / BeanLocations.xml],因为它不存在

时间:2011-11-19 11:35:50

标签: java spring maven

这很奇怪,我在Eclipse中运行项目没有问题但是如果运行则会失败     java -jar xxx.jar(导出到Runnable Jar)

    Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStore
    Exception: IOException parsing XML document from class path resource [spring/config/BeanLocations.xml]; 
    nested exception is java.io.FileNotFoundException: classpath resource [spring/config/BeanLocations.xml] cannot be opened because it does not exist
            at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
    nDefinitions(XmlBeanDefinitionReader.java:349)
            at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
    nDefinitions(XmlBeanDefinitionReader.java:310)
            at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
    r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
            at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
    r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
            at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
    r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
            at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
    r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
            at org.springframework.context.support.AbstractXmlApplicationContext.loa
    dBeanDefinitions(AbstractXmlApplicationContext.java:113)
            at org.springframework.context.support.AbstractXmlApplicationContext.loa
    dBeanDefinitions(AbstractXmlApplicationContext.java:80)
            at org.springframework.context.support.AbstractRefreshableApplicationCon
    text.refreshBeanFactory(AbstractRefreshableApplicationContext.java:123)
            at org.springframework.context.support.AbstractApplicationContext.obtain
    FreshBeanFactory(AbstractApplicationContext.java:422)
            at org.springframework.context.support.AbstractApplicationContext.refres
    h(AbstractApplicationContext.java:352)
            at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
            at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
            at my.lincdoc.manager.ProposalManager.getAllProposalInfo(ProposalManager
    .java:183)
            at my.lincdoc.manager.ProposalManager.eliminateDuplicate(ProposalManager
    .java:194)
            at my.lincdoc.controller.ProposalController.RetrieveProposalForm(Proposa
    lController.java:70)
            at my.lincdoc.common.App.main(App.java:16)
    Caused by: java.io.FileNotFoundException: class path resource [spring/config/Bea
    nLocations.xml] cannot be opened because it does not exist
            at org.springframework.core.io.ClassPathResource.getInputStream(ClassPat
    hResource.java:143)
            at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
    nDefinitions(XmlBeanDefinitionReader.java:336)
            ... 16 more

我将BeanLocations.xml放在src/main/resources/spring/config/文件夹

ApplicationContext appContext =  new ClassPathXmlApplicationContext("spring/config/BeanLocations.xml"); 
ProposalFormBo proposalFormBo = (ProposalFormBo) appContext.getBean("proposalBo");

1 个答案:

答案 0 :(得分:1)

确保您在jar中包含非类资源。解压你的jar并确保文件在那里(我敢打赌它不存在)。您需要调整生成jar的方式以包含它。