简体   繁体   English

如何从Java中的类路径加载“ context.xml”?

[英]How to load “context.xml” from classpath in Java?

I am trying to load my spring context.xml file using Java from the classpath. 我正在尝试使用Java从类路径中加载spring context.xml文件。 My context.xml file is in src/main/resources . 我的context.xml文件位于src/main/resources

public static void main(String[] args) {
    ClassPathXmlApplicationContext context = null;
    try {
        context = new ClassPathXmlApplicationContext("classpath:context.xml");
        context.registerShutdownHook();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (context != null) {
            context.close();
        }
    }
}

Above code works fine when I am running in my eclipse. 当我在日食中运行时,以上代码可以正常工作。 But as soon as I package it into a runnable jar by right clicking the project and select export as a runnable jar and then run the jar from a ubuntu machine as - 但是,只要我通过右键单击项目将其打包到可运行的jar中,然后选择export作为可运行的jar,然后从ubuntu机器上运行该jar即可-

java -jar test.jar

Then I am getting below exception - 然后我变得异常-

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: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:130)
        at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
        at com.host.sd.corp.ldap.Application.main(Application.java:63)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:616)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
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:157)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
        ... 18 more

Is there anything I am missing? 我有什么想念的吗?

You probably didn't include the resources in src/main/resources in your jar. 您可能没有在jar中包含src/main/resources中的src/main/resources I don't believe the Maven plugin is involved in Eclipse's Jar Export utility. 我不认为Maven插件与Eclipse的Jar Export实用程序有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM