Passing Command Line Arguments in Java – Spring Boot Example
Application properties are part of each and every application. To make your project robust and easy for the configuration you always think of keeping some properties either in the file, database or you can pass them as Command Line Arguments in Java. In this small article, we will quickly look into how you can leverage Spring Boot and its capabilities.
Command Line Arguments in Java – Spring Boot Example
By default, Spring comes with inbuilt options parser in the form of the ApplicationArguments interface. This interface will make sure that all you get to access command line parameters easily. By default, all command line parameters that start with — are converted to options. We will see the same using a simple example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package com.opencodez; import java.util.Arrays; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ArgsDemo implements ApplicationRunner { private static final Logger logger = LoggerFactory.getLogger(ArgsDemo.class); public static void main(String[] args) { SpringApplication.run(ArgsDemo.class, args); } @Override public void run(ApplicationArguments args) throws Exception { logger.info("Command-line arguments: {}", Arrays.toString(args.getSourceArgs())); logger.info("Non Option Args: {}", args.getNonOptionArgs()); logger.info("Option Names: {}", args.getOptionNames()); for (String name : args.getOptionNames()){ logger.info("arg-" + name + "=" + args.getOptionValues(name)); } } } |
In the above example, our Spring Boot Application is implementing the ApplicationRunner interface. With this interface, we must implement the run method to which we can pass command-line arguments as ApplicationArguments. Once we have access to arguments you can iterate over them and process. Every command-line argument with format
1 |
--variable.name=variable.value |
is converted to Option Name and Option Value. All other parameters passed to the program will be treated as Non Option Arguments. Let’s run the above program with below command-line arguments
1 |
non-option-arg --cmd.prop.1="Prop 1" --cmd.prop.2="Prop 2" |
And here is the output (after removing unnecessary console log)
1 2 3 4 5 6 |
Command-line arguments: [non-option-arg, --cmd.prop.1=Prop 1, --cmd.prop.2=Prop 2] Non Option Args: [non-option-arg] Option Names: [cmd.prop.2, cmd.prop.1] Argument = cmd.prop.2=[Prop 2] Argument = cmd.prop.1=[Prop 1] Started ArgsDemo in 6.795 seconds (JVM running for 7.825) |
As usual Spring Boot has made the development easy and enjoyable.
Command Line Arguments Precedence
Passing command-line arguments in java is one of the approaches taken to externalize your application configuration. Spring Boot supports such many approaches and has strict precedence in order to allow sensible overriding of values. In our case, if any property is defined in application properties and a similar option name is passed via command line, then the value from application properties will be overwritten.
You can find complete precedence order in Spring Official Documentation.
Lets test this precedence. Below is our properties file
1 2 |
spring.main.banner-mode=off app.property=application.properties |
We will add a simple rest controller to output our property from Spring Environment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.opencodez; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ArgsController { @Value("${app.property}") private String prop; @GetMapping public String hello(){ return "Hello, I am Property from " + prop + "!"; } } |
and when we run our application with and without app.property on command line we will see below
output:
I hope you find this example useful and do not hesitate to ask questions or comment.