本文介绍了使用带有多个参数的 Runtime.getRuntime().exec的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我尝试使用 Runtime.getRuntime().exec 从 Java 代码执行 openview 命令.这个确切的命令在服务器上的命令提示符下运行良好,会执行必要的更新,但在通过 Java 代码执行时无法执行.问题是它在通过 Java 调用时返回成功的退出状态代码,即0",但不执行它应该做的更新(看起来它没有执行).
So I trying to use Runtime.getRuntime().exec to execute a openview command from Java code. This exact command runs fine on command prompt on the server does the necessary updates, but fails to perform when executed through Java code. The issue is that it returns exit status code of success i,e "0" when invoked through Java, but doesn't performs the updates it is suppose to do (appears like it is not executing).
这是命令:
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);
推荐答案
这行得通:
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\' " }
这篇关于使用带有多个参数的 Runtime.getRuntime().exec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!