Search code examples
javadefault-valuesystem-properties

Set Java systemProperties for all Java Processes


I have a system Property (JVM Provided) called networkaddress.cache.ttl. This setting has a bad default of -1 and should be set to a different value (e.g.: 60).

I know that this can be set by calling java -Dnetworkaddress.cache.ttl=60 -jar main.jar

Is there a way to set this value as a system-wide default, such that any process running java -jar main.jar would pick it up?


Solution

  • Check JAVA_TOOL_OPTIONS environment variable - https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/envvars002.html

    You can set it to add JVM options to all Java processes.

    In your case it should be set to

    -Dnetworkaddress.cache.ttl=60
    

    JVM process should output at the beginning:

    Picked up JAVA_TOOL_OPTIONS: <your value>
    

    If they pick your setting.

    edit: alex

    public class Main {
      public static void main(String[] args) {
        System.out.println(System.getProperties().get("foobar"));
      }
    }
    

    Test:

    $ export JAVA_TOOL_OPTIONS='-Dfoobar=1'
    $ java Main
    Picked up JAVA_TOOL_OPTIONS: -Dfoobar=1 
    1