Runtime.exec() 将无法正确完成

发布于 2025-01-08 04:08:43 字数 1939 浏览 0 评论 0原文

我创建了一个 Web 应用程序,它依赖外部脚本来收集用户的查询请求(内部软件)。我已经成功地测试了 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
    }
}

}

有什么建议吗?预先感谢您的任何帮助

I've created a WebApp that relies on external scripts to gather query request by the user (internal software). I've tested, with sucess, the WebApp off the glassfish server provided by netbeans but whenever I try and upload my App to a third party server (Apache Tomcat) I run into the issue of the process.getRuntime exitValue never being written and the WebApp never gets to the result page....

This is the code that I have implemented so far:

Update --> The code now works after reading both stderr and 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
    }
}

}

Any Suggerstions? Thanks in advance for any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北笙凉宸 2025-01-15 04:08:43

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

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

The process may not be complete when you call exitValue() on it.

Before process.exitValue() call add:
process.waitFor();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文