How to use CommandLineRunner in Spring Boot Application?

Share on:

You must be wondering what is this “CommandLineRunner Interface” and why it is there in Spring Boot. So in this article I will be addressing the need of “CommandLineRunner Interface” as well as how and when to use it.

GitHub Source Code - Clone Repo

Spring Boot provides two Interface (Remember its an interface)

  1. CommandLineRunner
  2. ApplicationRunner

Since this article is about CommandLineRunner interface so we will take ApplicationRunner in another article.

Now back to the topic, since CommandLineRunner is an interface so we must implement the CommandLineRunner interface into our class.

Name : - CommandLineRunner
Type : - Interface
Overriding method : - run()

Why do we need CommandLineRunner? To answer the need of CommandLineRunner -Let’s say you have need to run Scheduled batch Job, set some system environment properties or need to perform some DB operation just before the Spring Boot run() method is finish, so in this kind of scenario CommandLineRunner Interface comes handy. It allows you do such operation before the Spring Boot’s run() method finishes its execution.

Lets Jump to Code
As per API documentation CommandLineRunner is an Interface, so we can not create an instance of it but we can implement it.

 1package com.jhooq.springbootcommandlinerunner;
 3import org.slf4j.Logger;
 4import org.slf4j.LoggerFactory;
 5import org.springframework.boot.CommandLineRunner;
 6import org.springframework.boot.SpringApplication;
 7import org.springframework.boot.autoconfigure.SpringBootApplication;
10 * Author : Rahul Wagh
11 **/
13public class SpringBootCommandLineRunner implements CommandLineRunner {
15    private static Logger LOG = LoggerFactory
16            .getLogger(SpringBootCommandLineRunner.class);
18    public static void main(String[] args) {
19"STARTING : Spring boot application starting");
20, args);
21"STOPPED  : Spring boot application stopped");
22    }
24    @Override
25    public void run(String... args) throws Exception {
26"EXECUTING : command line runner");
28        for(int i=0;i<=10;i++){
29  "Count ="+i);
30        }
31    }

Console Output

 1[main] c.j.s.SpringBootCommandLineRunner: Starting SpringBootCommandLineRunner on Rahul-PC with PID 1776 
 2[main] c.j.s.SpringBootCommandLineRunner: No active profile set, falling back to default profiles: default
 3[main] c.j.s.SpringBootCommandLineRunner: Started SpringBootCommandLineRunner in 1.815 seconds (JVM running for 3.31)
 4[main] c.j.s.SpringBootCommandLineRunner: EXECUTING : command line runner
 5[main] c.j.s.SpringBootCommandLineRunner: Count =0
 6[main] c.j.s.SpringBootCommandLineRunner: Count =1
 7[main] c.j.s.SpringBootCommandLineRunner: Count =2
 8[main] c.j.s.SpringBootCommandLineRunner: Count =3
 9[main] c.j.s.SpringBootCommandLineRunner: Count =4
10[main] c.j.s.SpringBootCommandLineRunner: Count =5
11[main] c.j.s.SpringBootCommandLineRunner: Count =6
12[main] c.j.s.SpringBootCommandLineRunner: Count =7
13[main] c.j.s.SpringBootCommandLineRunner: Count =8
14[main] c.j.s.SpringBootCommandLineRunner: Count =9
15[main] c.j.s.SpringBootCommandLineRunner: Count =10
16[main] c.j.s.SpringBootCommandLineRunner: STOPPED  : Spring boot application stopped

When to use CommandLineRunner?
CommandLineRunner can be used in the following scenarios : -

  1. Need to add some additional logger information
  2. Schedule a batch job
  3. Database operation i.e. cleanup script, status update

Gotcha for option and nonoption argument
CommandLineRunner doesn’t care about the option and nonoption argument, it will take all the args as array of string. Take a closer look at the output.

1[main] c.j.s.SpringBootCommandLineRunner: EXECUTING : command line runner
2[main] c.j.s.SpringBootCommandLineRunner: ARGS : - [--arg1=Jhooq, --arg2=CommanLineRunner]
3[main] c.j.s.SpringBootCommandLineRunner: STOPPED  : Spring boot application stopped

As you can see from the above console output, CommandLineRunner can not distinguish between the following parameters

  1. -arg1=Jhooq
  2. -arg2=CommanLineRunner All the above three arguments are treated as Array of String.

But not to worry Spring Boot provides one more interface ApplicationRunner which can be used to accomplish or identify the optional and non-optional arguments. For more details about ApplicationRunner please refer to : - Application Runner

I hope this little tutorial might have helped you to get better understanding on “How to use CommandLineRunner Interface”.
If you have any questions or queries please put your comments below and i will be happy to answer you.