【发布时间】:2015-06-15 16:10:56
【问题描述】:
我们有用于 WebSphere 队列的 python 应用程序。此应用程序由两个文件(queue-tools、queue-tools.py)和几个库组成。 queue-tools 文件如下所示:
#!/bin/sh
$HOME/sh/tests/queue-tool.py $@
当我在 unix 主机上运行 queue-tool -h 命令时,我看到了下一个结果:
queue-tool.py [-h] [-t host] [-q qmgr] [-c channel] [-s SecurityExit] [-w]
[-f [FILE]] [-put arg [arg ...]] [-get arg [arg ...]]
[-peek arg [arg ...]] [-inquire arg]
Queue Tool
optional arguments:
-h, --help show this help message and exit
-t host target host
-q qmgr queue manager
-c channel channel
所以我的问题是如何从 java 代码运行这个命令 (queue-tools -h) 并读取输出结果?我使用下面的代码,但它不起作用:
public static void testScript() {
String line;
try {
String[] cmd = {"/bin/sh", "-c", "queue-tool -h"};
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
BufferedReader in =
new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = in.readLine()) != null) { //line is null so nothing to read
System.out.println(line);
}
in.close();
} catch (InterruptedException iEX) {
iEX.printStackTrace();
} catch (IOException ioEX) {
ioEX.printStackTrace();
}
}
【问题讨论】:
-
去掉 p.waitFor()。如果我没记错的话,它会让它完成这个过程然后返回到你的线程,之后不会捕获任何输出。
-
@MiltoxBeyond p.waitFor() 返回 127 值。
-
@MiltoxBeyond 能否请您添加有关此问题的答案,只需添加上面的链接,我将更新此问题作为已回答。
-
@Paul 我只是把完整路径放到队列工具脚本中,就像这样
{ "/bin/sh", "-c", "/apps/scripts/sh/tests/queue-tool -h"}