A
Anony Usenet
Hi All,
I need to start and later kill a .bat script using Java 1.4 on windows
2000.
The .bat runs a .exe which must also be killed. (I can't run the .exe
directly)
When I call Process.destroy() on the .bat 'cmd' process the .exe is
not killed.
Is this proper behavior? Is there any way to do it?
The basic idea is show in the following code.
When run, notepad is not killed and the jvm does not exit until it is
killed manually.
##
## runNotepad.bat
##
notepad.exe
##
## TestExec.java
##
public class TestExec {
public static void main(String [] args)
throws Exception {
String execString = "cmd /c runNotepad.bat";
System.out.println("Starting process");
Process p = Runtime.getRuntime().exec(execString);
System.out.println("Wait for a few seconds");
Thread.sleep(5000);
System.out.println("Destroying process");
p.destroy();
System.out.println("Waiting for process to exit");
p.waitFor();
System.out.println("Exit value = " + p.exitValue());
// Notepad is still running
}
}
##
## Results
##
Starting process
Wait for a few seconds
Destroying process
Waiting for process to exit
Exit value = 1
Thanks for your help!
mike
I need to start and later kill a .bat script using Java 1.4 on windows
2000.
The .bat runs a .exe which must also be killed. (I can't run the .exe
directly)
When I call Process.destroy() on the .bat 'cmd' process the .exe is
not killed.
Is this proper behavior? Is there any way to do it?
The basic idea is show in the following code.
When run, notepad is not killed and the jvm does not exit until it is
killed manually.
##
## runNotepad.bat
##
notepad.exe
##
## TestExec.java
##
public class TestExec {
public static void main(String [] args)
throws Exception {
String execString = "cmd /c runNotepad.bat";
System.out.println("Starting process");
Process p = Runtime.getRuntime().exec(execString);
System.out.println("Wait for a few seconds");
Thread.sleep(5000);
System.out.println("Destroying process");
p.destroy();
System.out.println("Waiting for process to exit");
p.waitFor();
System.out.println("Exit value = " + p.exitValue());
// Notepad is still running
}
}
##
## Results
##
Starting process
Wait for a few seconds
Destroying process
Waiting for process to exit
Exit value = 1
Thanks for your help!
mike