直接粘贴代码示例:
Runtime rt = Runtime.getRuntime();
path = "c:\IBM\WebSphere\AppServer\profiles\STSCDmgrProfile\bin\";
cmd = path + "wsadmin";
String cmdString = cmd
+ " -host "
+ host
+ " -port "
+ port
+ " -username "
+ username
+ " -password "
+ password
+ " "
+ "-f" + "c:/IBM/WebSphere/AppServer/profiles/STSCDMgrProfile/temp/mergedScripts.jy"
+ " -lang "
+ lang
+ " -tracefile logs/ssc_wsadmin_trace.txt -appendtrace true";
_logger.finer(cmdString.replaceAll(" " + password, " <password>"));
Process proc = rt.exec(cmdString);
_logger.finer("Launched process");
stdInput = new BufferedReader(new InputStreamReader(proc
.getInputStream()));
stdError = new BufferedReader(new InputStreamReader(proc
.getErrorStream()));
// read the output from the command
String sIn = "";
**while ((sIn = stdInput.readLine()) != null) {**
_logger.log(Level.FINE, "runJCommand stin ==>", sIn);
}
但是从stdInput执行readLine()时它会挂起。上面突出显示。以下是我在跟踪日志中看到的内容:
[7/30/13 23:48:04:937 GMT-12:00] 000000a6 ThreadMonitor W WSVR0605W: Thread "WebContainer : 1" (00000175) has been active for 664085 milliseconds and may be hung. There is/are 1 thread(s) in total in the server that may be hung.
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:223)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:267)
at java.io.BufferedInputStream.read(BufferedInputStream.java:328)
at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(StreamDecoder.java:464)
at sun.nio.cs.StreamDecoder$CharsetSD.implRead(StreamDecoder.java:506)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:234)
at java.io.InputStreamReader.read(InputStreamReader.java:188)
at java.io.BufferedReader.fill(BufferedReader.java:147)
at java.io.BufferedReader.readLine(BufferedReader.java:310)
**at java.io.BufferedReader.readLine(BufferedReader.java:373)**
at com.ibm.sametime.console.admin.plugins.wsadmin.SSCWsAdmin.runJCommand(SSCWsAdmin.java:924)
如果我运行相同的mergedScripts.jy
(我在上面的java代码中调用),通过命令行手动运行,那么它会成功执行并在几分钟内完成。
但是,通过Java代码,它会永远运行。
可能的原因是什么?在上述情况下,STDIN实际上是什么?
答案 0 :(得分:0)
在您的示例代码中,您正在从InputStream中读取,而不是从ErrorStream中读取,您的问题可能是ErrorStream的缓冲区已满,这导致进程阻塞或死锁。请参阅“为什么Runtime.exec()挂起”部分in this article。本文接着描述了如何避免这种情况,但您可以使用ProcessBuilder而不是Runtime.exec()
来简化流程,然后重定向ErrorStream。
ProcessBuilder pb = new ProcessBuilder("wsadmin", "-host", host, "-port", port, "-username", username, ... );
Process proc = pb.redirectErrorStream(true).start();
//Read from the InputStread as you were...
在上述情况下,STDIN究竟是什么?
proc.getInputStream()
将包含运行wsadmin
命令时从命令行运行它时产生的任何输出。
答案 1 :(得分:0)
我不确定你的编码是什么,但我曾经写过类似于你在perl中所做的事情。也许你可以将它改编为python。
my $line = "cd /usr/websphere/profiles/myProfile/bin;";
$line .= ($user !~ /root/i) ? ' sesudo' : '';
$line .= " ./wsadmin.sh -lang jython ";
$line .= "-f $wasscriptpth ".join(' ', @args);
$line .= " 2>&1";
然后我只是在shell上调用命令并将输出传送到文件句柄...
open(SSHANDLE, "$line |")
注意&#34; 2&gt;&amp; 1&#34;这是在一个地方读取错误和标准输出的一种非常简单的方法。也许你可以在python中做这样的事情?