Detecting process termination

I need to detect when a process created by IDEA terminates so I can collect coverage data (my own - not EMMA's)
I mean process run as an Application or JUnit (or any other unit test framework), no matter if Run or Debugged.

The most logical tool seems to be to add a ProcessListener to a ProcessHandler.
Queston, though: how to lay hands on one?

One approach I used is to provide a RunConfigurationExtension instance and install a ProcessListener in the handleStartProcess method.
This works fine for Application, but is ignored in case of JUnit run.

Second one was to register a RunContentListener, extract ProcessHandler from RunContentDescriptor in contentSelected method and install the listener there (unless the process is already terminated or the listener is already installed)
This seems to work well for both Application and JUnit, but it depends on the fact that every time there is something written to the console - which seems to be true, but is it guaranteed? (And it generally seems hacky)

Is there any better way to do it?

Comment actions Permalink


Were you able to figure out the best way to install a ProcessListener?  I have a similar issue, execpt that I want to listen for a particular (kind of) process starting.


Comment actions Permalink

I needed that for similar purpose, but I use my own ProgramRunner derived from DefaultJavaProgramRunner which can install the listener in onProcessStarted() - executionResult.getProcessHandler().addProcessListener()

For the general purpose ("foreign" runners) I use the technique I described - it seems to work fine
I implement a RunContentListener registered via ExecutionManager.getInstance(project).getContentManager().addRunContentListener(monitor)
In the contentSelected() method I install the listener on ProcessHandler extracted from the descriptor, but there is not much info available.

I would really appreciate a tip how I actuallly should do that - my solution is a hack

Comment actions Permalink

Thanks for your reply!

I guess I will go with the approach you have outlined as it seems to be the only option.  I might also try having my own ProgramRunner for when/if I launch confgurations from my plug-in.


Please sign in to leave a comment.