David Hofmann
David Hofmann

Reputation: 5775

Gracefuly stop .net initiated Java Process

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 !

Upvotes: 1

Views: 585

Answers (1)

daveb
daveb

Reputation: 76241

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.

Upvotes: 2

Related Questions