运行Exec和一个定制的RTF编辑器(Runtime exec and a custom built

2019-09-26 16:08发布

我有一个管理RTF文档的创建和在该类调用的,用于显示一个XML文件中的RTF编辑器方法的类。

所有但一个用户可以没有任何问题访问此编辑器。 这一个用户始终运行到哪里他们的应用程序只是挂起的问题。 在任何的日志中没有错误。 通常,这样的问题是很容易识别,复制和纠正,但是,我不能为我再生的生命就这样我在尝试调试失败了。

基本代码如下:

int exitVal = CUBSRTFEditor.runRTFEditor("c:\\tmp\\control"+ap_doc_id+".xml", xml,"I:\\AppealsLetters.exe /process \"c:\\tmp\\control"+ap_doc_id+".xml\"");

public static int runRTFEditor(String xmlLocation, String xmlContent, String executePath)
{
    int exitVal = 0;
    createLocalFile(xmlLocation, xmlContent);

    try
    {
        System.out.println("executePath must = "+executePath);
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec(executePath);
        System.out.println("after executePath runs");

        //exhaust that stream before waiting for the process to exit
        InputStream inputstream = proc.getInputStream();
        InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
        BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
        // read the ls output
        String line;
        while ((line = bufferedreader.readLine())!= null)
        {
            System.out.println(line);
        }
        exitVal = proc.waitFor();
    }
    catch (Throwable t)
    {
        t.printStackTrace();
    }

    CUBSRTFEditor.deleteTempFile(xmlLocation);

    return exitVal;
}

最后的输出是第一的System.out。 当我把XML文件并执行此任何其他PC上执行没有问题。 我看到proc.getErrorStream()或proc.getOutputStream()没有任何有用的信息。

在这个问题上(EXEC吊)JDK的Javadoc文档: 由于某些本地平台只为标准输入和输出流提供有限的缓存器大小,未能及时写输入流或读出的子过程的输出流可能会导致子阻塞,甚至死锁。

我尝试排出该流等待进程退出之前似乎并没有帮助,因为它似乎从来就没到这一点(不显示第二的System.out)

有我实现了这个错误? 我失去了一些重要的东西? 如何获得更多的信息出来的过程中的任何想法将是巨大的。

我坚持....

Answer 1:

的Runtime.exec()是一个看似讨厌的小土豆一起工作。 我发现这篇文章 (老了,但仍然具有现实意义)是相当有帮助的。 您可以随时跳到第4页一些高度gankable示例代码。 :-)

一目了然,你的代码需要同时处理proc.getOutputStream()和proc.getErrorStream(),这是一个很好的理由来处理在单独的线程的溪流。



Answer 2:

我想更新这个,因为变化投产今天的工作。 基于断BlairHippo的建议我把它与一个匿名内部类工作,以创建一个单独的线程用尽两个错误和输入流。

new Thread(new Runnable(){
    public void run()
    {
        try
        {
            BufferedReader br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
            String line;
            while ((line = br.readLine())!= null)
            {
                System.out.println(line);
            }
        }
        catch (Throwable t)
        {
            t.printStackTrace();
        }
    }
}).start();


文章来源: Runtime exec and a custom built RTF editor