问题
Through System.getProperty(String key) you can get the value of some java system properties, however my question is about how java fill these values, more exactly where is the source of this information. I'm looking in some places like here but I didn't found an answer.
My problem is that I'm trying to test some behaviours of my code, and I want to change the system property for os.name
value outside of java (I know that I can set the value with System.setProperty(String key, String value) however I want to do it outside), so anybody knows what I've to change in order to get the new value through System.getProperty('os.name')
?
Thanks!
回答1:
I don't believe you should modify the built-in System Properties, the linked Java Tutorial says (in part)
The
System
class maintains aProperties
object that describes the configuration of the current working environment.
and
Warning: Changing system properties is potentially dangerous and should be done with discretion. Many system properties are not reread after start-up and are there for informational purposes. Changing some properties may have unexpected side-effects.
finally
To modify the existing set of system properties, use
System.setProperties
. This method takes aProperties
object that has been initialized to contain the properties to be set.
回答2:
You can pass a VM argument as
-Dos.name="Ubuntu 10.1"
If you're running Eclipse, you can specify the value through Run > Run configurations... Select your program under, say, Java Application and the providing the value in the Arguments tab.
回答3:
You can run java it with the option -Dos.name="XXX"
so System.getProperty('os.name')
will return 'XXX'.
来源:https://stackoverflow.com/questions/28047773/which-are-the-source-of-java-to-fill-system-properties