[ACCEPTED]-Scope of the Java System Properties-system-properties

Accepted answer
Score: 51

Scope of the System properties

At least from reading the API Specifications 32 for the System.setProperties method, I was unable to get an 31 answer whether the system properties are 30 shared by all instances of the JVM or not.

In 29 order to find out, I wrote two quick programs 28 that will set the system property via System.setProperty, using 27 the same key, but different values:

class T1 {
  public static void main(String[] s) {
    System.setProperty("dummy.property", "42");

    // Keep printing value of "dummy.property" forever.
    while (true) {
      System.out.println(System.getProperty("dummy.property"));
      try {
        Thread.sleep(500);
      } catch (Exception e) {}
    }
  }
}

class T2 {
  public static void main(String[] s) {
    System.setProperty("dummy.property", "52");

    // Keep printing value of "dummy.property" forever.
    while (true) {
      System.out.println(System.getProperty("dummy.property"));
      try {
        Thread.sleep(500);
      } catch (Exception e) {}
    }
  }
}

(Beware 26 that running the two programs above will 25 make them go into an infinite loop!)

It turns 24 out, when running the two programs using 23 two separate java processes, the value for the 22 property set in one JVM process does not 21 affect the value of the other JVM process.

I 20 should add that this is the results for 19 using Sun's JRE 1.6.0_12, and this behavior 18 isn't defined at least in the API specifications 17 (or I haven't been able to find it), the 16 behaviors may vary.

Are there any tools to monitor runtime changes

Not to my knowledge. However, if 15 one does need to check if there were changes 14 to the system properties, one can hold onto 13 a copy of the Properties at one time, and compare 12 it with another call to System.getProperties -- after all, Properties is 11 a subclass of Hashtable, so comparison would be performed 10 in a similar manner.

Following is a program 9 that demonstrates a way to check if there 8 has been changes to the system properties. Probably 7 not an elegant method, but it seems to do 6 its job:

import java.util.*;

class CheckChanges {

  private static boolean isDifferent(Properties p1, Properties p2) {
    Set<Map.Entry<Object, Object>> p1EntrySet = p1.entrySet();
    Set<Map.Entry<Object, Object>> p2EntrySet = p2.entrySet();

    // Check that the key/value pairs are the same in the entry sets
    // obtained from the two Properties.
    // If there is an difference, return true.
    for (Map.Entry<Object, Object> e : p1EntrySet) {
      if (!p2EntrySet.contains(e))
        return true;
    }
    for (Map.Entry<Object, Object> e : p2EntrySet) {
      if (!p1EntrySet.contains(e))
        return true;
    }

    return false;
  }

  public static void main(String[] s)
  {
    // System properties prior to modification.
    Properties p = (Properties)System.getProperties().clone();
    // Modification of system properties.
    System.setProperty("dummy.property", "42");
    // See if there was modification. The output is "false"
    System.out.println(isDifferent(p, System.getProperties()));
  }
}

Properties is not thread-safe?

Hashtable is thread-safe, so I was expecting that Properties would 5 be as well, and in fact, the API Specifications 4 for the Properties class confirms it:

This class is 3 thread-safe: multiple threads can share 2 a single Properties object without the need for 1 external synchronization., Serialized Form

Score: 13

System properties are per-process. This 8 means that they are more global than static 7 fields, which are per-classloader. So for 6 instance, if you have a single instance 5 of Tomcat running multiple Java webapps, each 4 of which has a class com.example.Example with a static field 3 named globalField, then the webapps will share system 2 properties, but com.example.Example.globalField can be set to a different 1 value in each webapp.

Score: 5

There is one copy of the properties per 8 VM. They have much the same problems as 7 other statics (including singletons).

I guess, as 6 a hack, you call System.setProperties to a version of Properties that 5 responds differently depending on the context 4 (thread, call stack, time of day, etc.). It 3 could also log any changes with System.setProperty. You could 2 also set a SecurityManager logs security checks for the 1 relevant permissions.

Score: 4

Yes, "system properties" are per-VM 8 (though there are a number of "magic" properties 7 that contain information about the host 6 system, eg: "os.name", "os.arch", etc.).

As 5 for your second question: I am not aware 4 of such a tool, but if you're concerned 3 abut system properties getting changed you 2 could use a special SecurityManager to prevent (and perhaps 1 even track) system property changes.

Score: 3

Their scope is the running JVM, but unless 4 you have some esoteric class loader issues 3 a static variable with a properties object 2 will do the same thing, and with the opportunity 1 to syncronize or do whatever else you need.

Score: 0

When you start a new JVM, it makes a copy 14 of enviroment variables and uses them for 13 all life-cycle of it. So if you makes changes 12 to that enviroment, they would remain confined 11 to it. The strange behavior I have encountered 10 and on which I am investigating is slightly 9 different: if I start a JVM declaring some 8 of the environment variables (-D argument 7 on cmd line) that affect the behavior of 6 libraries I use in my applications. But 5 if, inside the java code (where they are 4 visible), I make changes to them, it seems 3 that these changes do not affect the behavior 2 of libraries. The strange is that we are 1 in the same JVM!

More Related questions