I am working on an application that needs to launch a process and wait for its output. Sometimes the process crashes (very often,) but is not really an issue since I have mitigation tasks. The problem is that Windows detects the process crashed and prompts for user input, to either check for a solution online, or just close the program.
I tried to solve this by waiting for the process to complete in a Runnable
submitted to an ExecutorService
and using the Future
returned to specify a timeout. Speed is not really a concern for the application, and the external process is supposed to run for just a couple of seconds.
This is the code I am using:
final Process process = ...
final ExecutorService service = Executors.newSingleThreadExecutor();
try {
final Future<?> future = service.submit(new Runnable() {
@Override
public void run() {
try {
process.waitFor();
} catch (InterruptedException e) { /* error handling */}
}
});
future.get(10, TimeUnit.SECONDS);
} catch (final TimeoutException e) {
// The process may have crashed
process.destroy();
} catch (final Exception e) {
// error handling
} finally {
service.shutdown();
}
The code above worked well, but the crash dialog still pops up and it doesn't go away without user interaction.
This question presents a similar problem but from a .Net perspective and proposes to suppress the pop up through the Windows registry, which I cannot do, given that its effect is global to all process in the machine.
- Is there a way to prevent the dialog from being displayed at all?
or
- Is there a way to detect the application crash and handle it directly from Java without needing user interaction?
Additional details:
- I don't have the source of the external process.
- The external process is a console based application (i.e. no GUI.)
- Preferably I'm looking for a pure Java based solution (no JNI.)
Thank you.