Home > Software design >  configure spring boot applicatition using command line argument
configure spring boot applicatition using command line argument

Time:05-07

So far I have found 2 methods to configure spring boot application at startup, one uses -D and the other one uses -- like this:

java -jar -Dspring.profiles.active=test app.jar
java -jar --spring.profiles.active=test app.jar

Sometimes, the first works, sometimes the second works. As a developer found, when run method in the following example is not given args parameter, -- method does not work, but -D does.

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

While today, I was configuring logback at command line, and found that -Dlogback.configurationFile=/full_path/logback.xml does not work but --logging.config=file:logback.xmldoes.

Above may have other reason, maybe logback.configurationFile is wrong, but it acts like -D and -- difference.

Then my question is what is -D and --, is it Java or Spring thing? why it varies between not working and working?

Thanks.

CodePudding user response:

The -D java option sets a system property value as a couple property, value while by default SpringApplication converts any command line option arguments (starting with --, e.g. --spring.profiles.active=test) to a property and adds it to the Spring environment.

I'm seeing a possible cause of not predictable behaviour in the wrong position of the command line option like the line java -jar --spring.profiles.active=test app.jar you included because all command line options should stand after the jar like so the command should be rewritten as java -jar app.jar --spring.profiles.active=test to ensure the correct conversion into property by the Spring framework.

For the logback problem the --logging.config=file:logback.xml command line options argument is working because is it equal to override the value of the logging.config common application property present in the Spring framework, the other logback.configurationFile option is not a Spring common property so it is not automatically handled by the framework.

CodePudding user response:

To explain this answer, simply open Command prompt or terminal window & type

$ java --help
Usage: java [options] <mainclass> [args...]
           (to execute a class)
   or  java [options] -jar <jarfile> [args...]
           (to execute a jar file)
  ...
 Arguments following the main class, source file, -jar <jarfile>,
 -m or --module <module>/<mainclass> are passed as the arguments to
 main class.

 where options include:
    ...
    -D<name>=<value>
                  set a system property
    ...

Based on this help doc, -D & -- is from java :

  • -D is an option used to set system property(can be called as VM arguments) & -- is an argument[s] that is passed to main class; can be called as program arguments.
  • That is reason when your run is not given args parameter, -- is not working but -D does .
  • Regarding logback issue, VM argument won't work because its not managed by spring framework but passing logging.config as program argument does; SO reference link.
  • Related