有没有办法将系统属性转移到子流程?

Byu*_*Lee 5 java

我正在编写一个程序,以下列模式形成一个新的子流程:

proc = java.lang.Runtime.getRuntime().exec("java -jar Xxx.jar");
Run Code Online (Sandbox Code Playgroud)

虽然环境变量会自动继承到子流程,但我认为定义的系统属性-D<name of property>=<value of the property>不是.

我的问题是,如果有任何方法以编程方式传输系统属性.欢迎任何评论或答案.谢谢.

Byu*_*Lee 3

我提出的一个解决方案是定义一组属性来传递给子进程,并-D<key>=<value>从中创建一个字符串。

static String[] properties_to_pass = {
    "log4j.configuration"
};
Run Code Online (Sandbox Code Playgroud)

以上是要传递的系统属性集。然后...

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 );
    }
}
Run Code Online (Sandbox Code Playgroud)

而经过上述...

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

非常幼稚的解决方案,但无论如何都有效。但仍然不确定是否有更好的解决方案。欢迎任何进一步的评论。