how to kill a .exe started from a .bat cmd process

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
 
S

Steve W. Jackson

: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 don't understand why you can't run Notepad directly. I can.

As to the question...I'm unable to say why the destroy() doesn't
actually terminate the executable when run via batch like this. But I
did an experiment that does indeed work.

Instead of using a String, use a String array, like this:

String[] execString = { "notepad.exe" };

Changing only that line, run it again. It works for me. If you include
the "cmd" and "/c" portions, it won't work. But just notepad,exe does.

= Steve =
 
R

Roedy Green

When I call Process.destroy() on the .bat 'cmd' process the .exe is
not killed.

experiment with the start command in your bat file to launch. You
want a WAIT style start. That might give you power to kill it via the
Process object.
 
A

Andy Fish

in NT, just because process A started process B doesn't means it "owns" it.

in unix, the concept of process groups and some fancy goings on with signals
generally (though not necessarily) cause child processes to die when a
parent dies.

if you can't keep track of what sub- and sub-sub proceses might be started
by the shell script, AFAIK your only way to find them all is to use the
process API in windows. this will tell you the pid which started any other
pid.

I haven't done this for a long time, but in NT4 I recall using pstree.exe
and psapi.dll

HTH

Andy

Steve W. Jackson said:
: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 don't understand why you can't run Notepad directly. I can.

As to the question...I'm unable to say why the destroy() doesn't
actually terminate the executable when run via batch like this. But I
did an experiment that does indeed work.

Instead of using a String, use a String array, like this:

String[] execString = { "notepad.exe" };

Changing only that line, run it again. It works for me. If you include
the "cmd" and "/c" portions, it won't work. But just notepad,exe does.

= Steve =
 
K

Kin C. Wong

I was trying to do the same thing, but was unsucessful. I think the
reason why the String array works is that when you use "notepad.exe"
you are starting notepad directly and therefore the Java program has
the correct pid to kill it. When you start it from a batch file,
you're probably killing the process for the batch file. But the Java
program probably has no way to get to the pid of the program started
by the batch file.

If you were able to do this, I'd like to know how.

Thanks,
Kin
 
Joined
Mar 18, 2011
Messages
2
Reaction score
0
kill the process

hi,

I am running one process, it will call one more process.
now i can able to kill the first process, how can i kill the second process.

Thanks in advance,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top