Which are the source of java to fill system properties

…衆ロ難τιáo~ 提交于 2019-12-10 22:44:31

问题


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 a Properties 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 a Properties 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!