Java batch simple helloworld repeat example (without dependencies)

java_ee_logo_vert_v2In Java, the development of batch can be achieved with few solutions. Most applications use their own code from scratch. Here’s a quick example of full java batch, without any external dependecy.

Without using any Framework, you need to develop every time the same parts of the batch: arg validation, batch processing, and error handling. Another example with the SpringBatch follow.

The focus of this tutorial is to develop a batch that will perform an “HelloWorld” task as many times as requested (value specify in argument content).

1. How it works?

    new String[] { "10" } // exec 10 times

2. Demo (console output)

Executing batch
input args ok
Executing job
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Ending job (success)
Ending batch

3. Implementation


public class HelloWorldBatch {

    public static void main(String[] args) {
        System.out.println("Executing batch");

        // arg validation
        if (args != null && args.length == 1 && args[0] != null) {
            System.out.println("input args ok");
        } else {
            System.out.println("Error with input args");
            throw new IllegalArgumentException("Error with input args");

        // batch processing
        try {
            System.out.println("Executing job");
            for (int i = 0; i < Integer.valueOf(args[0]); i++) {

                // task to repeat
                System.out.println("Hello World!");
            System.out.println("Ending job (success)");
        } catch (Exception e) {

            // error handling
            System.out.println("Error during job (failure)");
            throw new RuntimeException("Error during task (failure)", e);
        System.out.println("Ending batch");


View source code and more tests

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your 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