Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use environment variables vs. system properties?

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?

like image 870
James Raitsev Avatar asked Dec 24 '12 23:12

James Raitsev


People also ask

What is difference between system properties and environment variables?

Properties are contained only within the Java platform, while Environment Variables are global at the Operating System level, available to all applications running on the same machine.

When would you use an environment variable?

The primary use case for environment variables is to limit the need to modify and re-release an application due to changes in configuration data.

Why do we use system property?

The System class maintains a Properties object that describes the configuration of the current working environment. System properties include information about the current user, the current version of the Java runtime, and the character used to separate components of a file path name.

Should I use environment variables?

Using environment variables is a somewhat common practice during Development but it is actually not a healthy practice to use with Production. While there are several reasons for this, one of the main reasons is that using environment variables can cause unexpected persistence of variable values.


2 Answers

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

like image 188
ruakh Avatar answered Sep 20 '22 14:09

ruakh


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.

like image 39
Nathan Ryan Avatar answered Sep 16 '22 14:09

Nathan Ryan