Destroying a Process in Java

J

Jerome

Hi there,

I am trying to destory a process that I have created in Java:

p1.destroy();
....

the destroy method does not work and is an abstract method. I was wondering
if there
is an alternative, like a class that extends Process, to kill a process?


Thanks in advance.
J.
 
S

Skip

Jerome said:
I am trying to destory a process that I have created in Java:

p1.destroy();

the destroy method does not work and is an abstract method.

It doesn't matter if it's abstract once you have an instance, as in that
case there will be an implementation.
 
T

Thomas Fritsch

Jerome said:
Hi there,

I am trying to destory a process that I have created in Java:

p1.destroy();
...

the destroy method does not work and is an abstract method.
It *does* work! You already *have* a concrete Process object returned
from your Runtime.exec call (probably of class Win32Process or
UnixProcess, which is a subclass of Process)
I was wondering
if there
is an alternative, like a class that extends Process, to kill a process?
You do not need an alternative. Just call p1.destroy() !
 
J

John C. Bollinger

Jerome said:
I am trying to destory a process that I have created in Java:

p1.destroy();
...

This is generally a bad idea. If at all possible, do not create
processes that you cannot allow to run to completion.
the destroy method does not work and is an abstract method.

As others have pointed out, it does work (for some definition of "work")
and is concrete on the class of the Process instance that you get your
hands on. You can never have an object whose class is abstract (though
its class may have abstract superclasses).

It is possible that whatever process you have created does not respond
to the OS-defendant technique that your Process instance uses to try to
destroy it. This is not a Java-specific or even OS-specific problem,
and is one reason why it is a bad idea to depend on being able to kill a
process by means of Process.destroy().


John Bollinger
(e-mail address removed)
 
L

Larry Barowski

I am trying to destory a process that I have created in Java:
p1.destroy();
...

the destroy method does not work and is an abstract method. I was wondering
if there
is an alternative, like a class that extends Process, to kill a process?

As others have said, it does work for most processes most of
the time. If you really need to kill processes, one guaranteed
way is to insert a native launcher program between Java and
the target (on "unknown" systems, you could just launch the
process directly). Usually this would be a two-process system.
When the first process dies, the second one will detect that
and shut down the target - first in a "nice" way, then if that
doesn't work after x number of seconds, in a "not nice" way
which is highly likely to work. The first process will be set up
so it will always shut down upon request, and simple enough
that it is unlikely to ever "lock up". If the target finishes, the
second process will shut down the first. Creating such a
launcher program that behaves correctly in all circumstances
and passes I/O through the processes (if necessary) is not
simple on any OS.
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top