【问题标题】:Java Spring Batch - executing from a jarJava Spring Batch - 从 jar 执行
【发布时间】:2023-07-25 19:37:01
【问题描述】:

我创建了一个春季批处理项目。它作为 Java 应用程序完美执行。之后,我使用 maven build 创建了一个 jar 文件。但是当我尝试运行 jar 文件时,会抛出以下异常:

=================================================================================
Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 142 in XML document from class path resource [SICBatchFile.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'job'.
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)

Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'job'.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    ... 14 more

=================================================================================

它正在通过邮件类。但是在读取 xml 时,它会抛出这个异常。下面是xml部分:

  <beans:beans 
        xmlns="ht tp: / /www.springframework.org/schema/batch" 
        xmlns:beans="ht tp: / /www.springframework.org/schema/beans" 
        xmlns:aop="ht tp: / /www.springframework.org/schema/aop"
        xmlns:tx="ht tp: / /www.springframework.org/schema/tx" xmlns:p="ht tp: / /www.springframework.org/schema/p"
        xmlns:xsi="ht tp :/ /www.w3.org/2001/XMLSchema-instance" xmlns:util="ht tp: / /www.springframework.org/schema/util"
        xsi:schemaLocation="ht tp: / /www.springframework.org/schema/beans  ht tp: / /www.springframework.org/schema/beans/spring-beans-2.0.xsd
        ht tp: / /www.springframework.org/schema/batch  ht tp: / /www.springframework.org/schema/batch/spring-batch-2.0.xsd
        ht tp: / /www.springframework.org/schema/aop  ht tp: / /www.springframework.org/schema/aop/spring-aop-2.0.xsd
        ht tp: / /www.springframework.org/schema/tx 
        ht tp: / /www.springframework.org/schema/tx/spring-tx-2.0.xsd">

     <job id="sicProcessorJob" job-repository="jobRepository">
        <step id="step1">
        <tasklet transaction-manager="jobRepository-transactionManager">
            <chunk reader="sicFileItemReader" processor="sicProcessor"
                writer="sicFileItemWriter" commit-interval="3">
        <streams>
            <stream ref="sicFileItemWriter1"/>
            <stream ref="sicFileItemWriter2" />
          </streams>
        </chunk>    
    </tasklet>
    </step>
    <listeners>
      <listener ref="jobListener" />
    </listeners>
  </job>

IDE:Eclipse

【问题讨论】:

标签: java spring spring-batch


【解决方案1】:

之后,我使用 maven build 创建了一个 jar 文件。但是当我试图 运行jar文件

如果你只是创建了一个简单的 jar 文件,那么它就无法工作,应用程序需要所有库(spring、commons 等)

你可以

  • 设置classpath 或使用manifest 配置它
  • 使用maven创建(见* answer
    • 可执行的 all-in-one.jar
    • 包含您需要的所有目录结构,包括设置正确类路径的 .bat/.sh

【讨论】: