Centurion, South Africa info@code2bits.com On Request

How to Create a Console Application using CommandLineRunner (Part 1)

In this guide, I will show you how to create a simple console application using Spring Boot. A console application is a computer program designed to be used via a text-only computer interface, such as a the command line interface. This example show you how to create a Spring Boot application and make use of the CommandLineRunner to read values from the the command line and print them out.

Getting Started

The following list defines the technologies and libraries I used to implement the sample code:

Multiple Posts

This will form part of a multi-part series on how to create a console application using the CommandLineRunner.

Code Example

The code example can be downloaded from Github from the button below. After you have downloaded the code onto your machine, you can either work through the guide step-by-step to understand the example, or jump to the “Compile & Run The Example” section to get the example up and running.

Download Code

Step 1: Define Maven Dependencies

The following dependencies should be included in the pom.xml file:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

Step 2: Implement the CommandLineRunner Interface

The console application is implemented by a class called SimpleConsoleApplication. The class contains the @SpringBootApplication annotation that enables the spring boot auto configuration mechanism, enables the component scan on the packages and allow to register extra beans in the context.

The SimpleConsoleApplication class also implements the CommandLineRunner interface. This Interface is used to indicate that a bean should run when it is contained within a SpringApplication. The run method will be invoked after the application context has been loaded.

The run method prints the arguments it recieved as arguments what the application was started.

@SpringBootApplication
public class SimpleConsoleApplication implements CommandLineRunner {

    private static Logger LOG = LoggerFactory.getLogger(SimpleConsoleApplication.class);

    public static void main(String[] args) {
        LOG.info("Spring Application Start");
        SpringApplication.run(SimpleConsoleApplication.class, args);
        LOG.info("Spring Application Complete");
    }

    @Override
    public void run(String... args) {
        LOG.info("Printing Command Line Arguments: ");

        for (int i = 0; i < args.length; ++i) {
            LOG.info("args[{}]: {}", i, args[i]);
        }   
    }
}

Step 3: Compile and Run the Application

Use the following command to compile the Spring Boot application making use of maven.

Build Application

$ mvn clean install

After you have successfully build the Spring Boot application, the compiled artifact can be found in the target directory. There are two ways to run the Spring Boot Application.

Run Application

$ mvn spring-boot:run -Dspring-boot.run.arguments=Hello,World
$ java -jar target/simple-0.0.1-SNAPSHOT.jar Hello World

The following display illustrate how to first build the Spring Boot application, and then start the simple console application by passing in the two arguments, "Hello" and "World".

Summary

Congratulations! You have successfully created a simple console application using Spring Boot.