Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to transfer system properties to sub-processes?

Tags:

java

I'm writing a program that forms a new sub-process in a following pattern:

proc = java.lang.Runtime.getRuntime().exec("java -jar Xxx.jar");

Though the environment variables are automatically inherited to sub-processes, I think the system properties defined by -D<name of property>=<value of the property> are not.

My question is, if there is any way to transfer the system properties programmatically. Any comments or answers are welcomed. Thanks.

like image 842
Byungjoon Lee Avatar asked Sep 22 '14 02:09

Byungjoon Lee


1 Answers

One solution that I've come up with is to define a set of properties to pass to subprocesses, and create a -D<key>=<value> strings from it.

static String[] properties_to_pass = {
    "log4j.configuration"
};

Above is the set of system properties to pass. Then...

StringBuffer properties = new StringBuffer();
for ( String property : properties_to_pass ) {
    String value = System.getProperty(property);
    if ( value != null ) {
        String r = String.format("-D%s=%s ", property, value);
        properties.append( r );
    }
}

And after the above ...

String command_arg = properties.toString();
String command = String.format("java %s -jar Torpedo.jar", command_arg);
java.lang.Runtime.getRuntime.exec( command );

Very naive solution, but works anyway. But still not sure that there might be a better solution. Any further comments are welcomed.

like image 84
Byungjoon Lee Avatar answered Oct 17 '22 05:10

Byungjoon Lee