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.
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:
- Using
argsinmainmethod:
In this case, the args array allows you to access the command-line arguments directly.
- Using
ApplicationArgumentsBean:ApplicationArgumentsis a Spring Framework interface that provides a convenient way to access command-line arguments. Here's how you can use it:
The ApplicationRunner interface provides the run method where you can retrieve and process arguments.
- Using
CommandLineRunnerBean:CommandLineRunneris another interface to access command-line arguments. The main difference betweenApplicationRunnerandCommandLineRunneris that the former provides parsed arguments viaApplicationArguments, whereas the latter provides raw arguments.
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:
- Via Maven properties: Set properties in the
pom.xmlunder thebuildplugin configuration:
This flexibility allows you to drive application configuration from outside without altering application code.
Summary Table
| Approach | Description | Example |
args in main method | Direct access to CLI args in the main method. | SpringApplication.run(App.class, args); |
ApplicationRunner | Provides parsed args through ApplicationArguments; easier parsing. | Access via run(ApplicationArguments args) |
CommandLineRunner | Provides raw CLI args for more manual parsing. | Access via run(String... args) |
| Maven Arguments | Specify 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
- Get first and last day of month using threeten, LocalDate
- Get first or last entry of a LinkedHashMap
- Get generic type of java.util.List
- Get integer value of the current year in Java
- Get java.nio.file.Path object from java.io.File
- Get keys from HashMap in Java
- Get last element of Stream/List in a one-liner
- Get list of JSON objects with Spring RestTemplate

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.