Spring Boot Console Application

In this tutorial, you will learn how to create a console-based Spring Boot application that you can run in the terminal window on your computer. You will also learn to pass command-line arguments to your Spring Boot console-based application.

For more Spring Boot tutorials and video lessons check this page: Spring Boot tutorials and video lessons.

Create a New Spring Boot Application

If you do not have a Spring Boot application created, you can follow this tutorial on how to create a basic Spring Boot application(Includes a video tutorial). Although it does not need to be a Web application. So you may not add any of Spring Boot Web dependencies. In fact, let’s create it as a non-web application. So the only dependencies in the pom.xml file of your non-web Spring Boot console-based application will be as follows:

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

 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-devtools</artifactId>
  <scope>runtime</scope>
  <optional>true</optional>
 </dependency>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
  <exclusions>
   <exclusion>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
   </exclusion>
  </exclusions>
 </dependency>
</dependencies>

Implement CommandLineRunner Interface

Once your basic Spring Boot application is created, open the Java class, which contains the public static void main(String agrs[]) method. You will notice that this class has a @SpringBootApplication annotation above the class name.

  • Open the class that is annotated with @SpringBootApplication annotation,
  • Make it implement the CommandLineRunner interface,
  • Override the public void run(String… args) method.

Here is an example of this Java class with the CommandLineRunner interface implemented.

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

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

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

 @Override
 public void run(String... args) throws Exception {
  System.out.println("Running Spring Boot Application");
  }

}

Run Spring Boot Application with the Command Line

You can run your Spring Boot application using your Java IDE or the Command Line in the terminal window.

Open the terminal window and change the directory to the root folder of your Spring Boot application. If you list files in this directory, you should see a pom.xml file.

Run using the Maven command

To run the Spring Boot application using the Maven command, run the following command:

mvn spring-boot:run

Run as an executable Jar file

You can also run your Spring Boot application as an executable Java jar file. To do that, follow these steps:

  • Run mvn package command to package the Spring Boot application as an executable Jar file,
  • In the target folder, you will see a Jar file with the name of your application. Usually, it will end with -0.0.1-SNAPSHOT.jar.  Run java -jar target/<application name>-0.0.1-SNAPSHOT.jar. The name of my Spring Boot application is CommandLineRunner. So here is how I run it as an executable Java file:
mvn package
java -jar target/commandlinerunner-0.0.1-SNAPSHOT.jar

Pass Command-Line Arguments

When running the Spring Boot application, you can pass command-line arguments. Here is an example of how to do it.

mvn spring-boot:run -Dspring-boot.run.arguments=--firstName=Sergey,--lastName=Kargopolov

Read Command-Line Arguments

To read the command line arguments passed to your Spring Boot application, simply iterate over the array of args. Below is a code example of how to do it.

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

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

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

 @Override
 public void run(String... args) throws Exception {
  System.out.println("Running Spring Boot Application");
   if (args.length > 0 ) {
    for (String arg : args) {
     System.out.println("Command line argument: " + arg);
    }  
   }
 }
}

Make Beans Run Code

You can also make your Java class annotated with @Component annotation, implement the CommandLineRunner interface and make it run some code once the bean has been created. For example, let’s assume we have a Java class called RunnerOne. We will make it implement the CommandLineRunner interface and run some code right after this bean has been created.

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(value=1)
public class RunnerOne implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Running Runner One");
    }
    
}

Notice the @Order annotation. The order annotation is needed if you have more than one Bean implementing the CommandLineRunner interface and you want your Java Beans to run code in order. Let’s create a second Bean and make it also implement the CommandLineRunner interface. The second Bean will have an order value of 2 and run only after the first bean has finished running its code.

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(value=2)
public class RunnerTwo implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Running Runner Two");
    }
    
}

I hope this tutorial was helpful to you. You now know how to build a very simple console-based Spring Boot application that you can run from the command line of your terminal window as an executable Jar file or with a maven command.

If you would like to learn more about Spring Boot, have a look at the below list of online video courses. One of them might help you learn more.

 

Leave a Reply

Your email address will not be published. Required fields are marked *