Dzmitry Sevkovich
Dzmitry Sevkovich

Reputation: 901

How to get an exception thrown from another application in Java?

I have the following code which prints the output of the DataIntegrationV8.jar to the JTextArea. Is it possible to print an exception thrown by that program in the same 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;
    }

Upvotes: 1

Views: 125

Answers (2)

Reimeus
Reimeus

Reputation: 159864

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");
}

Upvotes: 2

thatidiotguy
thatidiotguy

Reputation: 9011

You could check the error code returned by the program and it it is not zero, you know the output is a java stacktrace as the program terminated ...... poorly.

Upvotes: 0

Related Questions