gpt4 book ai didi

java - Runtime.exec() 不会正确完成

转载 作者:行者123 更新时间:2023-12-04 05:57:08 24 4
gpt4 key购买 nike

我创建了一个 WebApp,它依赖于外部脚本来收集用户(内部软件)的查询请求。我已经成功地测试了 netbeans 提供的 glassfish 服务器上的 WebApp,但是每当我尝试将我的应用程序上传到第三方服务器(Apache Tomcat)时,我都会遇到 process.getRuntime exitValue 从未被写入的问题,并且WebApp 永远不会进入结果页面....

这是我迄今为止实现的代码:

更新 --> 读取 stderr 和 stdin 后,代码现在可以工作:

        pd = new ProcessBuilder("runscript.bat");
pd.redirectErrorStream(true);
process = pd.start();


StreamGobbler inputGobbler = new StreamGobbler(process.getInputStream(), "Input");
StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(),"Error");

errorGobbler.start();
inputGobbler.start();

int exitVal = -1;

try {
exitVal = process.waitFor();
} catch (InterruptedException ex) {
//log Error
}

class StreamGobbler extends Thread {
OutputWriterReader outWR = new OutputWriterReader();
BufferedWriter deadWriter;
InputStream is;

// reads everything from is until empty.
StreamGobbler(InputStream is, String type) {
this.is = is;
createWatch(type);
}
// depending on if Error stream or Input Stream
private void createWatch(String type){
try {

if(type.equals("Error"))
deadWriter = new BufferedWriter(new FileWriter("StdError.txt"));
else if(type.equals("Input"))
deadWriter = new BufferedWriter(new FileWriter("StdInput.txt"));

} catch (IOException ex) {
//log Error
}
}

@Override
public void run() {
try {
InputStreamReader isr = new InputStreamReader(this.is);
BufferedReader br = new BufferedReader(isr);
String line=null;

while ( (line = br.readLine()) != null)
deadWriter.append(line);

deadWriter.flush();
deadWriter.close();

} catch (IOException ioe) {
//log Error
}
}

}

任何建议?在此先感谢您的帮助

最佳答案

当您对其调用 exitValue() 时,该过程可能未完成。

在 process.exitValue() 调用之前添加:
process.waitFor();

关于java - Runtime.exec() 不会正确完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9385309/

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