简体   繁体   中英

Difference between setting the runtime option using -D option when starting the application and setting using System.setProperty

What is the difference between the following two ways of setting property?

  1. While starting the application, eg java - cp /usr/bin/*.jar -D java.file.path=/home/export .

  2. System.setProperty("java.file.path","/home/export")

When I use the first method to set a path for 3rd party API, it's working fine, but the second method does not work.

I think that you would use -D if you want your filepath to be injectable from the person/process starting your app via jar. And the System.setProperty is just setting the system property. no science about it

In the first case, the system property is set when the JVM starts and before any code begins to execute. The second only takes effect after this line of code executes. If the value was read by the 3rd party lib earlier it won't see this value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM