Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 运行时exec和自定义构建的RTF编辑器_Java_Rtf_Runtime.exec - Fatal编程技术网

Java 运行时exec和自定义构建的RTF编辑器

Java 运行时exec和自定义构建的RTF编辑器,java,rtf,runtime.exec,Java,Rtf,Runtime.exec,我有一个类来管理RTF文档的创建,该类中有一个方法用XML文件调用RTF编辑器进行显示 除一个用户外,所有用户都可以访问此编辑器,而不会出现任何问题。这一用户始终遇到应用程序挂起的问题。任何日志中都没有错误。通常这类问题很容易识别、重现和纠正,但是,我一生都无法重现它,因此我调试的尝试都失败了 基本上,守则如下: int exitVal = CUBSRTFEditor.runRTFEditor("c:\\tmp\\control"+ap_doc_id+".xml", xml,"I:\\Appea

我有一个类来管理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()中没有看到有用的信息

JDK关于这个问题的Javadoc文档(exec挂起): 由于某些本机平台仅为标准输入和输出流提供有限的缓冲区大小,因此未能及时写入子流程的输入流或读取子流程的输出流可能会导致子流程阻塞,甚至死锁

我尝试在等待进程退出之前耗尽该流,但这似乎没有帮助,因为它似乎从未到达该点(第二个System.out未显示)

我是否错误地实现了这一点?我错过了什么重要的事情吗?任何关于如何从这个过程中获得更多信息的想法都是非常好的

我被卡住了……

Runtime.exec()是一个令人讨厌的小家伙。我发现(旧的,但仍然相关)非常有用。您可以随时跳到第4页,查看一些高度易懂的示例代码。:-)


乍一看,您的代码需要同时处理proc.getOutputStream()和proc.getErrorStream(),这是在单独的线程中处理这些流的一个很好的理由。

我想更新它,因为更改已在今天投入生产并生效。根据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();

+1用于链接。这是一个很好的链接,我已经从头到尾读了两遍。这是有用的,但没有什么帮助。我也添加了proc.getOutputStream(),但仍然没有成功;您还需要点击proc.getErrorStream()。最好是在各自的线程中。