Scope of the Java System Properties
Asked Answered
B

7

57

In Java we use System.setProperty() method to set some system properties. According to this article the use of system properties is bit tricky.

System.setProperty() can be an evil call.

  • It is 100% thread-hostile
  • It contains super-global variables
  • It is extremely difficult to debug when these variables mysteriously change at runtime.

My questions are as follows.

  1. How about the scope of the system properties? Are they specific for each and every Virtual Machine or they have a "Super Global nature" that shares the same set of properties over Each and every virtual machine instance? I guess the option 1

  2. Are there any tools that can be used to monitor the runtime changes for detect the changes in System properties. (Just for the ease of problem detection)

Beanie answered 26/5, 2009 at 4:17 Comment(0)
J
54

Scope of the System properties

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

In order to find out, I wrote two quick programs that will set the system property via System.setProperty, using 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 that running the two programs above will make them go into an infinite loop!)

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

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

Are there any tools to monitor runtime changes

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

Following is a program that demonstrates a way to check if there has been changes to the system properties. Probably not an elegant method, but it seems to do 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 be as well, and in fact, the API Specifications for the Properties class confirms it:

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

Jobye answered 26/5, 2009 at 4:44 Comment(0)
B
14

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

Blackdamp answered 30/8, 2013 at 0:4 Comment(1)
@Dejel They are not thread-local. All threads within the same JVM process have a shared set of system property values.Blackdamp
I
4

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

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

Inequality answered 26/5, 2009 at 4:38 Comment(0)
M
4

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

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

Mahmud answered 26/5, 2009 at 4:39 Comment(0)
M
3

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

Mulct answered 26/5, 2009 at 4:41 Comment(0)
G
0

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

Garver answered 26/10, 2017 at 7:23 Comment(1)
My problem has been solved reading this: https://mcmap.net/q/245572/-what-is-the-difference-between-setting-system-property-from-command-line-and-codeGarver
B
-1

You don't say what your motivation is for using system properties.

We use Spring for our configuration and set initial properties using a properties file that is injected into the XML. Changes to configuration whilst the app is running are made by using JMX.

There are - of course - many other ways to change configuration in Java using properties file, xml based configuration etc.

Blameless answered 26/5, 2009 at 5:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.