spring-boot
command-line-arguments
java
maven
application-development

Get command-line arguments from spring-bootrun

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Spring Boot is a powerful framework for building Java applications, making it easier to configure and deploy applications by simplifying the setup process with default settings for the framework. One of the essential tasks when running a Spring Boot application is passing command-line arguments which can be used to modify applications' behaviors or configurations dynamically. This article explains how to handle command-line arguments when running Spring Boot applications and showcases practical examples.

Accessing Command-Line Arguments in Spring Boot

When running a Spring Boot application, especially using the spring-boot:run command from Maven, you may need to access command-line arguments within your application code. Spring Boot provides several ways to achieve this:

  1. Using args in main method:
java
   public static void main(String[] args) {
       SpringApplication.run(MyApplication.class, args);
   }

In this case, the args array allows you to access the command-line arguments directly.

  1. Using ApplicationArguments Bean: ApplicationArguments is a Spring Framework interface that provides a convenient way to access command-line arguments. Here's how you can use it:
java
1   import org.springframework.boot.ApplicationArguments;
2   import org.springframework.boot.ApplicationRunner;
3   import org.springframework.stereotype.Component;
4
5   @Component
6   public class MyAppRunner implements ApplicationRunner {
7       @Override
8       public void run(ApplicationArguments args) throws Exception {
9           String[] sourceArgs = args.getSourceArgs();
10           boolean debug = args.containsOption("debug");
11           List<String> nonOptionArgs = args.getNonOptionArgs();
12           
13           System.out.println("Source Arguments: " + Arrays.toString(sourceArgs));
14           System.out.println("Debug Mode: " + debug);
15           System.out.println("Non-Option Arguments: " + nonOptionArgs);
16       }
17   }

The ApplicationRunner interface provides the run method where you can retrieve and process arguments.

  1. Using CommandLineRunner Bean: CommandLineRunner is another interface to access command-line arguments. The main difference between ApplicationRunner and CommandLineRunner is that the former provides parsed arguments via ApplicationArguments, whereas the latter provides raw arguments.
java
1   import org.springframework.boot.CommandLineRunner;
2   import org.springframework.stereotype.Component;
3
4   @Component
5   public class MyCommandLineApp implements CommandLineRunner {
6       @Override
7       public void run(String... args) throws Exception {
8           for (String arg : args) {
9               System.out.println("Command-line argument: " + arg);
10           }
11       }
12   }

The choice between ApplicationRunner and CommandLineRunner depends on whether you prefer working with raw or parsed command-line arguments.

Passing Command-Line Arguments with Maven

When using Maven's spring-boot:run goal, arguments can be passed in different ways:

  • Directly in the command:
bash
  ./mvnw spring-boot:run -Dspring-boot.run.arguments=--debug=true,--spring.profiles.active=dev
  • Via Maven properties: Set properties in the pom.xml under the build plugin configuration:
xml
1  <build>
2      <plugins>
3          <plugin>
4              <groupId>org.springframework.boot</groupId>
5              <artifactId>spring-boot-maven-plugin</artifactId>
6              <configuration>
7                  <arguments>
8                      <argument>--debug=true</argument>
9                      <argument>--spring.profiles.active=dev</argument>
10                  </arguments>
11              </configuration>
12          </plugin>
13      </plugins>
14  </build>

This flexibility allows you to drive application configuration from outside without altering application code.

Summary Table

ApproachDescriptionExample
args in main methodDirect access to CLI args in the main method.SpringApplication.run(App.class, args);
ApplicationRunnerProvides parsed args through ApplicationArguments; easier parsing.Access via run(ApplicationArguments args)
CommandLineRunnerProvides raw CLI args for more manual parsing.Access via run(String... args)
Maven ArgumentsSpecify CLI args during spring-boot:run using Maven config or command line.-Dspring-boot.run.arguments=--arg=value

Conclusion

Handling command-line arguments in Spring Boot is straightforward thanks to the available interfaces and tools. Whether you're starting out with Spring Boot or refining your application's deployment options, understanding these methods ensures your application's configuration is flexible and adaptable. As projects grow in complexity, leveraging these command-line capabilities can help manage application behaviors dynamically and efficiently.

By understanding and using these techniques effectively, you'll enhance the usability and configurability of your Spring Boot applications, delivering powerful Java applications with ease.


Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.