07
Apr
11

Spring Batch Executable JAR with Dependencies


This page describes how to create a self contained Spring Batch Application using Maven 2.

Background

Self contained jars are executable and can run independently. Maven includes all the runtime dependencies into these jars so that the jar’s can be run by executing the the “java -jar” command.

Requirements

We will continue where we left off in my last tutorial.

Add the following plugin to the pom.xml file

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>
                            org.springframework.batch.core.launch.support.CommandLineJobRunner
                        </mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>

Package the application

The following command packages the JAR file and includes all the runtime dependencies.

mvn clean compile assembly:single

Run the application

Open up a command line window and navigate to the project base folder. The self contained jar is located in the target folder. CD to the folder and execute the following.

java -jar springBatchHelloWorld-1.0-SNAPSHOT-jar-with-dependencies.jar simpleJob.xml helloWorldJob

You should notice a bunch of output including the “Hello World” message.

Advertisements

13 Responses to “Spring Batch Executable JAR with Dependencies”


  1. 1 jaya
    September 13, 2011 at 12:42 am

    hi,
    I am a newbie in spring ,I tried to package and run the hello world job example ,but facing the following error

    SEVERE: Job Terminated in error: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/batch]
    Offending resource: class path resource [simpleJob.xml]

    org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/batch]
    Offending resource: class path resource [simpleJob.xml]

  2. 2 NewToSBatch
    October 2, 2011 at 7:49 pm

    I am also getting the same above error ? Any idea Y this is happening . Please guide me

    • October 2, 2011 at 9:48 pm

      Just tried it the second time and everything works fine for me. Please ensure that you have the right version of spring in the classpath by verifying the dependencies in the pom.xml file. Also verify that you don’t have another version of spring in the classpath. The error you are getting sometimes happens when you have spring 1.x.x in your classpath. The xml namespace only appeared after spring batch 2.x.

  3. 4 rajaa
    January 6, 2012 at 1:42 pm

    I am getting the error while running the application:

    nested exception is java.io.FileNotFoundException: class path resource [simpleJob.xml] canno
    t be opened because it does not exist

  4. 7 Jon
    December 28, 2012 at 8:46 am

    If you are getting the above error regarding the Spring XML namespace, you will probably have to use the maven shade plugin. I added the approved answer to my pom, and ran mvn:shade:shade and it worked like a champ. See the following link: http://stackoverflow.com/questions/8523997/unable-to-locate-spring-namespacehandler-for-xml-schema-namespace-http-www-sp

  5. November 20, 2014 at 2:35 pm

    For the Javaconfig version:
    java -jar springBatchHelloWorld-1.0-SNAPSHOT-jar-with-dependencies.jar com.test.config.HelloWorldJobConfig helloWorldJob

  6. January 25, 2016 at 5:58 am

    how to configure the JobListener using java Configs
    please suggest me


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 77 other followers

April 2011
S M T W T F S
« Mar   May »
 12
3456789
10111213141516
17181920212223
24252627282930

Blog Stats

  • 830,821 hits

%d bloggers like this: