1

我有以下代码将 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;
    }
4

2 回答 2

2

By default exception stacktraces are displayed to System.err. You could include the output from the ErrorStream to the JTextArea.

BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String errorLine = null;
while ((errorLine = error.readLine()) != null) {
    jtaAbout.append(errorLine + "\n");
}
于 2013-05-29T17:47:00.417 回答
0

您可以检查程序返回的错误代码,它不为零,您知道输出是一个 java 堆栈跟踪,因为程序终止了......很糟糕。

于 2013-05-29T17:42:48.173 回答