I have a couple of problems using exec():
1) I'm running a program which produces output to both standard
output and standard error streams. How can I read from one or
the other, depending on which has been written to? At the moment
I read from the standard output stream, but this blocks if there
is nothing to be read and I never get a chance to read from the
standard error stream. As a workaround I use "2>&1" to combine
the streams, but might not always be able to do so easily (e.g.
if I want to redirect the standard output to a file).
2) I have a separate thread which calls destroy() on the Process
after 10 seconds to limit maximum execution time. If I have a
program which runs for 50 seconds, it calls destroy() correctly
after 10 seconds, but the call to waitFor() doesn't complete
until the full 50 seconds have elapsed, and then bad things
start happening (e.g. a database connection gets dropped for
no apparent reason). This is on Windows XP, which may be part
of the problem... :-)
Any ideas?
TIA,
-----------------------------------------------------------------
John English | mailto:je@brighton.ac.uk
Senior Lecturer | http://www.it.bton.ac.uk/staff/je
School of Computing & MIS | ** NON-PROFIT CD FOR CS STUDENTS **
University of Brighton | -- see http://burks.bton.ac.uk
-----------------------------------------------------------------
Roedy Green - 24 Oct 2005 13:11 GMT
>1) I'm running a program which produces output to both standard
> output and standard error streams. How can I read from one or
[quoted text clipped - 4 lines]
> the streams, but might not always be able to do so easily (e.g.
> if I want to redirect the standard output to a file).
Use separate three threads to write, and read each of the err and
output streams.
See http://mindprod.com/jgloss/exec.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
John English - 25 Oct 2005 09:59 GMT
>>1) I'm running a program which produces output to both standard
>> output and standard error streams. How can I read from one or
[quoted text clipped - 7 lines]
> Use separate three threads to write, and read each of the err and
> output streams.
Ah, good idea. Thanks.
-----------------------------------------------------------------
John English | mailto:je@brighton.ac.uk
Senior Lecturer | http://www.it.bton.ac.uk/staff/je
School of Computing & MIS | ** NON-PROFIT CD FOR CS STUDENTS **
University of Brighton | -- see http://burks.bton.ac.uk
-----------------------------------------------------------------
Roedy Green - 24 Oct 2005 13:17 GMT
>2) I have a separate thread which calls destroy() on the Process
> after 10 seconds to limit maximum execution time. If I have a
> program which runs for 50 seconds, it calls destroy() correctly
> after 10 seconds, but the call to waitFor() doesn't complete
> until the full 50 seconds have elapsed,
If you want the waitfor() to terminate early, you must call
thatThread.interrurupt() from some other thread.
waitfor() is sitting there waiting for either you or the child
spawned's task return to kick it off again.
see http://mindprod.com/jgloss/exec.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
John English - 25 Oct 2005 09:57 GMT
>>2) I have a separate thread which calls destroy() on the Process
>> after 10 seconds to limit maximum execution time. If I have a
[quoted text clipped - 4 lines]
> If you want the waitfor() to terminate early, you must call
> thatThread.interrurupt() from some other thread.
I seem to be doing that. My watchdog thread does this:
try {
sleep(timeout);
process.destroy();
parentThread.interrupt();
}
catch (InterruptedException e) { }
and the parent thread does this:
Process p = runtime.exec(command);
BufferedReader r = new BufferedReader(
new InputStreamReader(p.getInputStream())
);
int x = -1;
String s = null;
Watchdog w = new Watchdog(p,Thread.currentThread(),timeout);
w.start();
try {
while ((s = r.readLine()) != null) {
...
}
p.waitFor();
x = p.exitValue();
w.interrupt();
}
catch (InterruptedException e) {
...
}
-----------------------------------------------------------------
John English | mailto:je@brighton.ac.uk
Senior Lecturer | http://www.it.bton.ac.uk/staff/je
School of Computing & MIS | ** NON-PROFIT CD FOR CS STUDENTS **
University of Brighton | -- see http://burks.bton.ac.uk
-----------------------------------------------------------------
Dom Gilligan - 25 Oct 2005 21:21 GMT
John - burks.bton.ac.uk seems to have been offline for the past few
weeks; are you aware of this? Is it mirrored anywhere else?
Thanks -
Dom
John English - 27 Oct 2005 10:32 GMT
> John - burks.bton.ac.uk seems to have been offline for the past few
> weeks; are you aware of this? Is it mirrored anywhere else?
Ah, must change my .sig; the server has been decommissioned now.
-----------------------------------------------------------------
John English | mailto:je@brighton.ac.uk
Senior Lecturer | http://www.it.bton.ac.uk/staff/je
School of Computing & MIS |
University of Brighton |
-----------------------------------------------------------------