When to use environment variables vs. system properties?

后端 未结 4 1417
眼角桃花
眼角桃花 2021-02-05 23:56

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 som

4条回答
  •  野性不改
    2021-02-06 00:44

    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).

提交回复
热议问题