gpt4 book ai didi

java - 使用 Runtime.exec() 启动 Websphere 的 wsadmin.bat - 从进程的 InputStream 读取时挂起

转载 作者:行者123 更新时间:2023-11-29 09:00:04 25 4
gpt4 key购买 nike

直接贴代码示例:

        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 究竟是什么?

最佳答案

在您的示例代码中,您正在从 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...

What actually is STDIN in the above case ?

proc.getInputStream() 将包含从命令行运行 wsadmin 命令时产生的任何输出。

关于java - 使用 Runtime.exec() 启动 Websphere 的 wsadmin.bat - 从进程的 InputStream 读取时挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17988008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com