MartK
MartK

Reputation: 614

Log4j hanging my application

Im trying to get console output from an external application in my application. When i call the externall app from my code it hangs with the message:

Configuring logging...

Configuring log4j from: C:\GPAT\log4j.cfg

and nothing happens. I searched through internet and it seems that it might be thread issue. But i cant modify this external application and i must go through log4j. I read the external app like this:

StringBuffer output = new StringBuffer();

    try {
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec(GSATCommand);
        BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        System.out.println("Test running...");
        String line = null;
        while ((line = input.readLine()) != null) {
            System.out.println(line);   // Writes the test output to console
            output.append(line);  output.append("\n");
        }
        int exitVal = proc.waitFor();
        System.out.println("Process exitValue: " + exitVal);
        System.out.println("Test successfully executed");
    } catch (Throwable t) {
        t.printStackTrace();
    }

Thanks for reading.

Upvotes: 0

Views: 845

Answers (1)

Brian Agnew
Brian Agnew

Reputation: 272237

You need to consume both stdout and stderr from a spawned process in separate threads, to prevent blocking behaviour (your spawned process will write to buffers, and block if those buffers aren't being emptied by your consuming process).

See the 2nd paragraph of this answer for more details and a link to a suitable fix.

Upvotes: 2

Related Questions