1) java bat
Process p = new Process();
ProcessStartInfo si = new ProcessStartInfo();
si.Arguments = @"-jar app.jar";
si.FileName = "java";
p.StartInfo = si;
p.Start();
Console.WriteLine(p.Id);
if (!p.HasExited)
p.Kill();
, try..catch, WaitForExit() HasExited
, , " " .
, " " , ?
2) If you must use the bat file, then get the KillProcessAndChildren () method from programmatically process the process tree in C # (requires a link to System.Management) and call
KillProcessAndChildren(javaProcessID);
He will kill the main process and all his children.
3) And, of course, you can still use your source code and list all processes by name
Process[] pp = Process.GetProcessesByName("java");
foreach (Process p in pp)
p.Kill();
pp = Process.GetProcessesByName("cmd");
foreach (Process p in pp)
p.Kill();
but if you have several java / cmd processes, this will delete all of them, which may not be very good.
source
share