When to use environment variables vs. system properties?

↘锁芯ラ 提交于 2019-12-31 22:03:44

问题


I wonder which of the following is a preferred approach?

We can set things up as APP_HOME=/path/to/file (export in .profile or something along those lines) and access it as System.getenv("APP_HOME")

Or, alternatively using properties as -DAPP_HOME=/path/to/file and access it as System.getProperty("APP_HOME")

Now .. either one will make the value available for the application stand point, but is either approach preferred? Why? When?


回答1:


If you are using Java 1.3 or 1.4 (and 1.2, IIRC), you should be using system properties, since System.getenv was deprecated. It was reinstated in Java 1.5. The relevant bug report can be found here.

You can use both. Search system properties for the key, and if it's not there, search the environment. This gives you the best of both worlds.

These really aren't the same thing: One requires the value to be set explicitly, and the other not. Also, note that the environment is a convenient place to put some strings for interoperability.




回答2:


The Javadoc for System.getenv(String) addresses this question directly, saying:

System properties and environment variables are both conceptually mappings between names and values. Both mechanisms can be used to pass user-defined information to a Java process. Environment variables have a more global effect, because they are visible to all descendants of the process which defines them, not just the immediate Java subprocess. They can have subtly different semantics, such as case insensitivity, on different operating systems. For these reasons, environment variables are more likely to have unintended side effects. It is best to use system properties where possible. Environment variables should be used when a global effect is desired, or when an external system interface requires an environment variable (such as PATH).

(emphasis mine).




回答3:


Can't comment yet at the moment, so I will add a couple of points as an answer.

I agree with the javadoc's saying "It is best to use system properties where possible.", also in my own words previous to seeing this page here that the Java system variables are encapsulated inside the JVM. They are not visible to other processes on the host, and thus less coupled with the host system.

In addition, there are multiple interfaces to set the global environment variables, and so it could be a bit tricky to track all the values being used over time.




回答4:


One important difference between using Environment Variables (envs) and System properties which should be considered is that the envs could not be changed at runtime/in the running process, BUT System properties could be. Refer to the Javadoc:

https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#setProperties-java.util.Properties-



来源:https://stackoverflow.com/questions/14026558/when-to-use-environment-variables-vs-system-properties

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