如何在Java中获得从另一个应用程序抛出的异常



我有以下代码,它将DataIntegrationV8.jar的输出打印到JTextArea。是否有可能在同一个JTextArea中打印由该程序抛出的异常?

protected Integer doInBackground() throws Exception {
        Process process;
        InputStream iStream;
        try {
            //run the DataIntegration.jar
            process = Runtime.getRuntime().exec("java -jar DataIntegrationV8.jar sample.xml");
            istream = process.getInputStream();
        } catch (IOException e) {
            throw new IOException("Error executing DataIntegrationV8.jar");
        }
        //get the output of the DataIntegration.jar and put it to the 
        //DataIntegrationStarter.jar form
        InputStreamReader isReader = new InputStreamReader(iStream);
        BufferedReader bReader = new BufferedReader(isReader);
        String line;
        while ((line = bReader.readLine()) != null) {
            jtaAbout.append(line + "n");
        }
        Thread.sleep(1);
        return 42;
    }

默认情况下,异常堆栈跟踪显示给System.err。您可以将ErrorStream的输出包括到JTextArea

BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String errorLine = null;
while ((errorLine = error.readLine()) != null) {
    jtaAbout.append(errorLine + "n");
}

您可以检查程序返回的错误代码,如果它不为零,则您知道输出是java堆栈跟踪,因为程序终止......差。

相关内容

最新更新