使用带有多个参数的Runtime.getRuntime()。exec

时间:2018-04-19 06:05:40

标签: java runtime

所以我尝试使用Runtime.getRuntime()。exec从Java代码执行openview命令。这个确切的命令在服务器上的命令提示符下运行良好,执行必要的更新,但在通过Java代码执行时无法执行。问题是它返回成功的退出状态代码i,当通过Java调用时返回“0”,但不执行它想要做的更新(看起来好像没有执行)。

这是命令:

opcmsg application='Tester Down 11' object='My Support' severity=minor msg_grp='MyGroup' msg_text='DEV: -m=New Details:Request Detail description'

以下是代码:

String[] command = { 
    "opcmsg", 
    "application=\'Tester Down 11\'", 
    "object=\'My Support\'", 
    "severity=minor", 
    "msg_grp=\'MyGroup\'", 
    "msg_text=\'DEV: -m=New Details:Request Detail description\'" 
}
p = Runtime.getRuntime().exec(command);

InputStream stderr = p.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String errorDescription = null;

while ( (errorDescription = br.readLine()) != null)
    LOGGER.info(errorDescription);

    exitStatus = p.waitFor();
    LOGGER.info("exitStatus : " + exitStatus);

1 个答案:

答案 0 :(得分:0)

这有效:

  

String [] command = {“/ bin / sh”,       “-C”,       “opcmsg application = \'Tester Down 11 \'object = \'My Support \'severity = minor msg_grp = \'MyGroup \'msg_text = \'DEV:-m = New Details:Request Detail description''”}