【问题标题】:Spring Maven Standalone application Jar not workingSpring Maven 独立应用程序 Jar 不工作
【发布时间】:2016-08-31 14:52:36
【问题描述】:

我用 maven 创建了一个 Spring 独立应用程序。因此还添加了 maven 依赖项。

  • src 文件夹结构为 src/main/java 资源文件夹为 源/主/资源。
  • resources 文件夹包含 1)applicationContext.xml 2)application.properties。

如果我从命令行运行 jar,我会收到以下错误

 D:\>java -jar "myTest.jar"
Aug 31, 2016 7:20:21 PM org.springframework.context.support.ClassPathXmlApplicat
ionContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationCont
ext@74be95bf: startup date [Wed Aug 31 19:20:21 PKT 2016]; root of context hiera
rchy
Aug 31, 2016 7:20:22 PM org.springframework.beans.factory.xml.XmlBeanDefinitionR
eader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [resources/applicati
onContext.xml]
Exception in thread "main" java.lang.ExceptionInInitializerError
        at com.my.sftp.SFTPTester.main(SFTPTester
.java:78)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOExc
eption parsing XML document from class path resource [resources/applicationConte
xt.xml]; nested exception is java.io.FileNotFoundException: class path resource
[resources/applicationContext.xml] cannot be opened because it does not exist
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
nDefinitions(XmlBeanDefinitionReader.java:344)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
nDefinitions(XmlBeanDefinitionReader.java:304)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
        at org.springframework.context.support.AbstractXmlApplicationContext.loa
dBeanDefinitions(AbstractXmlApplicationContext.java:123)
        at org.springframework.context.support.AbstractXmlApplicationContext.loa
dBeanDefinitions(AbstractXmlApplicationContext.java:93)
        at org.springframework.context.support.AbstractRefreshableApplicationCon
text.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
        at org.springframework.context.support.AbstractApplicationContext.obtain
FreshBeanFactory(AbstractApplicationContext.java:612)
        at org.springframework.context.support.AbstractApplicationContext.refres
h(AbstractApplicationContext.java:513)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<i
nit>(ClassPathXmlApplicationContext.java:197)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<i
nit>(ClassPathXmlApplicationContext.java:172)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<i
nit>(ClassPathXmlApplicationContext.java:158)
        at com.my.sftp.ApplicationContextProvider.<clinit>(Applicat
ionContextProvider.java:10)
        ... 1 more
Caused by: java.io.FileNotFoundException: class path resource [resources/applica
tionContext.xml] cannot be opened because it does not exist
        at org.springframework.core.io.ClassPathResource.getInputStream(ClassPat
hResource.java:172)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
nDefinitions(XmlBeanDefinitionReader.java:330)
        ... 12 more

    Here are the few lines of pom.xml suggesting how i am adding "resources" folder reference

    <build>
       <resources>
       <resource><directory>src/main/resources</directory></resource>
       </resources>
      <pluginManagement>
       <plugins>
        <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-compiler-plugin</artifactId>
         <version>2.3.1</version>
         <configuration>
          <source>1.7</source>
          <target>1.7</target>

         </configuration>
        </plugin>
       </plugins>
      </pluginManagement>

这是 main() 代码

public static void main(String[] args)
   {
     ConfigurableApplicationContext context = null;
     try {
       context = new ClassPathXmlApplicationContext("resources/applicationContext.xml", SFTPTester.class);
       context.getBean("sftpTester");

     } catch (Exception e) {
       e.printStackTrace();
       context.close();
     }
     catch (Throwable t) {
         t.printStackTrace();
         context.close();
       }

   }

当前项目结构如上所示

【问题讨论】:

  • 我认为您只需要通过资源加载器打开文件“resources/applicationContext.xml”,因为它不能作为文件使用(通过 getClass().getResourceAsStream()....) .如果您不尝试读取或解析它,您可能只是配置了 spring-context 文件的位置错误?在这种情况下,请尝试删除资源/部分。 src/main/resources 中的所有内容都直接在类路径中结束。

标签: spring maven resources


【解决方案1】:

当您使用 maven jar 文件时,资源文件将直接位于根级别。您必须将 resources/applicationContext.xml 更改为 applicationContext.xml 才能使其正常工作。

附带说明,如果您想知道 Jar 文件中的内容,请将 .jar 更改为 .zip 并提取内容。您将对正在发生的事情有更好的了解。

【讨论】:

  • 如果是这样,这意味着如果我必须调试代码,那么我必须将资源文件夹的内容放在具有 main() 的类所在的同一文件夹中。因此,每次调试时,我都必须更改引用 applicationContext.xml 的代码行。
  • 我根据stackoverflow.com/users/258741/zeus的回复改了。但仍然存在错误。现在我可以在观察 jar 内容的同时在根目录看到我的 applicationContext.xml。
  • @ALI 你能粘贴加载xml文件的代码吗?
  • 我添加了引用applicationContext.xml的main方法。还提供了项目结构以跟随位置
  • @ALI 即使在调试时你也可以在没有资源/的情况下使用相同的路径mkyong.com/spring3/spring-3-hello-world-example
【解决方案2】:

除了在 java main() 中更改引用上下文的行解决了问题之外,一切都在问题中。现在项目在 Eclipse 调试以及 maven 生成的 jar 文件中运行良好。 所以我改行为

context = new ClassPathXmlApplicationContext("applicationContext.xml");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 2021-05-24
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    相关资源
    最近更新 更多