views:

126

answers:

1

Having the next .net code, I want to stop it correctly so that the jvm executes it's ShutdownHooks.

System.Diagnostics.Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.WorkingDirectory = @"C:\test\";
p.StartInfo.FileName = @"C:\Program Files\Java\jre6u16\bin\java.exe";
p.StartInfo.Arguments = "-jar TheExecutable.jar";

Trying to stop the process with p.Kill() will force the jvm to stop without sending the equivalent SIGTERM found in the unix kill comand. Or maeby I am doing something wrong because the java process does not get it's shutdown hooks executed.

Thanks !

+1  A: 

If you have control over the Java code, you could detect a fairly normal situation like End-Of-File in stdin, or a so-called poison pill in stdin, and use that to decide (in Java) that now is the time to exit.

Then to kill the java process, do whatever is needed to its stdin.

Another option is to write a signal handler, - see sun.misc.SignalHandler, and register this with sum.misc.Signal

Other options seem rather hideous - like polling the parent process or looking at a file.

daveb
Really useful answer. Thanks a lot !
David Hofmann
By the way, would you explain please why in windows in this specific case we can't do what in linux whould have work sending a SIGTERM ?
David Hofmann