Skip to content
Related Articles

Related Articles

What is Command Line Runner Interface in Spring Boot?

View Discussion
Improve Article
Save Article
  • Last Updated : 22 Nov, 2021

Java language is one of the most popular languages among all programming languages. There are several advantages of using the java programming language, whether for security purposes or building large distribution projects. One of the advantages of using Java is that Java tries to connect every concept in the language to the real world with the help of the concepts of classes, inheritance, polymorphism, etc.

There are several other concepts present in java that increase the user-friendly interaction between the java code and the programmer such as generic, Access specifiers, Annotations, etc these features add an extra property to the class as well as the method of the java program. In this article, we will discuss the command line runner interface in the spring boot.

CommandLineRunner Interface

This interface is present in the dependency of the springbootframework.boot that is used to quickly start the spring boot application. This interface contains run method() that is executed after the application startup. Geeks, if you do find it not easy to grasp then do have a prior understanding of Spring Initializr 

Method Invoked: Overrided run()

void run(String ..arg)

Parameter: This method contains varargs parameters for handling multiple string objects.

Return type: It doesn’t return anything(void)

We do use it because if it’s associative advantages of using command line runner interface as described. Debugging is very easy using the command line runner interface because we don’t need to explicitly call the particular resources via API calling we can call them using the run method of command-line runner interface that is executed immediately once the application is executed successfully.

Let’s understand the functionality of this interface using an example, so Spring Initializr is basically is a web-based tool using which we can easily generate the structure of the Spring Boot project. It also provides various different features for the projects expressed in a metadata model. This model allows us to configure the list of dependencies that are supported by JVM. Here, we will create the structure of an application using a spring initializer, therefore, to do this, the following steps as depicted below media sequentially as follows:

Steps:

  1. Go to Spring Initializr
  2. Fill in the details as per the requirements.
  3. Click on Generate which will download the starter project
  4. Extract the zip file.
  5. Go to SpringBootAppApplication class
  6. Run the SpringBootAppApplication class and wait for the Tomcat server

Step 1: Go to Spring Initializr

Step 2: Fill in the details as per the requirements. For this application:

Project: Maven
Language: Java
Spring Boot: 2.2.8
Packaging: JAR
Java: 8
Dependencies: Spring Web

Step 3: Click on Generate which will download the starter project.

Step 4: Extract the zip file. Now open a suitable IDE and then go to File->New->Project from existing sources->Spring-boot-app and select pom.xml. Click on import changes on prompt and wait for the project to sync.

Note: In the Import Project for Maven window, make sure you choose the same version of JDK which you selected while creating the project.

Step 5: Go to src -> main -> java -> SpringBootAppApplication class

SpringBootAppApplication.java

@SpringBootApplication

// Main class
// Implementing CommandLineRunner interface
public class SpringBootAppApplication implements CommandLineRunner 
{

    // Method 1
    // run() method for springBootApplication to execute
    @Override
    public void run(String args[]) throws Exception 
    {

        // Print statement when method is called
        System.out.println("HEllo world");
    }

    // Method 2
    // Main driver method
    public static void main(String[] args) 
    {

        // Calling run() method to execute SpringBootApplication by
        // invoking run() inside main() method
        SpringApplication.run(SpringBootAppApplication .class, args);
    }
}

This application is now ready to run. 

Step 6: Run the SpringBootAppApplication class and wait for the Tomcat server to start where the default port is already set.

Tip: The default port of the Tomcat server is 8080 and can be changed in the application.properties file.

Output: Generated on terminal/CMD 

If we want to print something we have to call using API that is why the command-line runner interface is helpful.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!