Java

Spring Boot CommandLineRunner Code Example

Spring Boot helps you quickly get started with building a Spring-based web app in no time. All you need to do is go to Eclipse IDE, create a new Spring Starter Project and that is it. This is provided you installed Eclipse plugin, Spring Tools (aka Spring IDE and Spring Tool Suite) by going to Help > Eclipse Marketplace… and searching for “spring tools” keyword.

Often, one wants to run a Spring Boot app as a Java application as like a traditional Java program and test different parts of their app.

In this post, you will get a template code for quickly getting started with Command Liner which would help you to execute your Spring boot app as a Java application.

Spring Boot App with CommandLineRunner

I create a spring started a project with the name as recruiterbot. I got the following code:

package com.vitalflux.rbot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class RecruiterbotApplication {

    public static void main(String[] args) {
        SpringApplication.run(RecruiterbotApplication.class, args);
    }
}

In order to work with command line, all you need to do is implement an interface CommandLineRunner. The following code represents the same.

@SpringBootApplication
public class RecruiterbotApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(RecruiterbotApplication.class);
        app.run(args);
    }

    @Override
    public void run(String... arg0) throws Exception {
        System.out.println("Hello World");
    }
}

To run the Spring Boot as Java application from within Eclipse, right-click on the program, RecruiterBotApplication and click Run As / Java Application.

References

Summary

In this post, you learned about getting started with Spring Boot app using CommandLineRunner.

Did you find this article useful? Do you have any questions or suggestions about this article in relation to Spring Boot app with CommandLineRunner? Leave a comment and ask your questions and I shall do my best to address your queries.

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

Recent Posts

Creating a RAG Application Using LangGraph: Example Code

Retrieval-Augmented Generation (RAG) is an innovative generative AI method that combines retrieval-based search with large…

23 hours ago

Building a RAG Application with LangChain: Example Code

The combination of Retrieval-Augmented Generation (RAG) and powerful language models enables the development of sophisticated…

2 days ago

Building an OpenAI Chatbot with LangChain

Have you ever wondered how to use OpenAI APIs to create custom chatbots? With advancements…

3 days ago

How Indexing Works in LLM-Based RAG Applications

When building a Retrieval-Augmented Generation (RAG) application powered by Large Language Models (LLMs), which combine…

7 days ago

Retrieval Augmented Generation (RAG) & LLM: Examples

Last updated: 25th Jan, 2025 Have you ever wondered how to seamlessly integrate the vast…

1 week ago

What are AI Agents? How do they work?

Artificial Intelligence (AI) agents have started becoming an integral part of our lives. Imagine asking…

4 weeks ago