Sometimes, we do not need the web component of Spring Boot in our application. To create such a non-web application, the main class must implement CommandLineRunner interface and override therun() method.


We only need spring-boot-starter dependency for console application. Here is how our build.gradle file looks like:


plugins {
    id 'org.springframework.boot' version '2.1.3.RELEASE'
    id 'java'

apply plugin: 'io.spring.dependency-management'

group = 'com.attacomsian'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'

I am using the Gradle plugin version 5.2.1 for this example. We used implementation keyword in the dependencies list instead of compile. compile configuration was depreciated starting from Gradle version 3.0.

If you are using Maven, please add the following dependency to your pom.xml file:


Spring Classes

Let's create a simple Service class that returns a message. It has an overloaded getMessage() method that returns the default message if no argument is passed. The default message is an external property declared in file.


import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

public class HelloService {

    private String message;

    public String getMessage() {
        return message;

    public String getMessage(String message) {
        return "Hey, " + message;

Now create the main class for our Spring Boot console application which implements the CommandLineRunner interface. This interface provides a simple run() method that is automatically invoked by Spring Boot after the application context has been loaded.

package com.attacomsian.console;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class ConsoleApplication implements CommandLineRunner {

    private HelloService helloService;

    public ConsoleApplication(HelloService helloService) {
        this.helloService = helloService;

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(ConsoleApplication.class);
        // disable spring banner

    public void run(String... args) throws Exception {
        //check if user passes any argument
        if (args.length > 0) {
        } else {
            //print the default message

In the code above, we are using the @SpringBootApplication annotation on our main class to enable the auto-configuration. The run() method is the entry point of our application. Inside this method, we first check if the user passes any command-line argument or not. Based on the argument availability, we call the corresponding HelloService method.

Another way to explicitly inform Spring Boot that this is not a web application is by using external properties. For example, we can specify the following properties in our file to disable the Spring Boot banner on startup. It will also not start the application as an embedded web server.



Execute the following command in your terminal to run the Gradle project:

$ cd go/to/your/project/directory
$ ./gradlew bootRun

If you want to pass a command-line argument, use --args flag.

$ ./gradlew bootRun --args "Mike"
Hey, Mike

Source code: Download the complete source code from GitHub available under MIT license.


That's all for creating a console (non-web) application in Spring Boot. We discussed several options available to explicitly inform Spring Boot that this is not a web application.

Most of the time, console applications are simple and have only one class that implements CommandLineRunner. But if you want to implement the CommandLineRunner interface more than once, you should use @Order annotation to specify their execution sequence.

Read Next: How to scaffold a Spring Boot application

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.