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.xml
does.
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?
CodePudding user response:
The -D
standard java option sets a system property value as a couple property, value while by default SpringApplication
converts any command line option argument (substantially String
values corresponding to String[] args
with the prefix --
, e.g. --spring.profiles.active=test
) to a property and adds it to the Spring environment eventually overriding the property with the same name if it exists.
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 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. Be aware that in the case your Spring application tries to read a value from the system while you are passing the property as a command line option argument the application will not work, same will happen when you sets the system property and the Spring application tries to read it as a command line property.
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 givenargs
parameter,--
is not working but-D
does . - Regarding
logback
issue, VM argument won't work because its not managed by spring framework but passinglogging.config
as program argument does; SO reference link.