How to stop an external process in a Java Program

J

Jerome

Hi there,

I have written some Java code that executes an external shell script that
runs a server. However, I don't know how to kill the process in Java and
thus can't reconnect to the server.

I have:
....
Runtime rt1 = Runtime.getRuntime();
Process p1 = rt1.exec("XXX.sh");

....

Does anyone know how to kill a process?
Thanks in advance.
AL
 
S

Stefan Schulz

Hi there,

I have written some Java code that executes an external shell script that
runs a server. However, I don't know how to kill the process in Java and
thus can't reconnect to the server.

I have:
...
Runtime rt1 = Runtime.getRuntime();
Process p1 = rt1.exec("XXX.sh");

...

Does anyone know how to kill a process?
Thanks in advance.

I bet the javadocs for Process do. Take a look, this class only has 6
methods that are not inherited, and the one you are looking for is named
pretty obviously.
 
A

Ann

Did you try something like this?

Runtime rt2 = Runtime.getRuntime();
Process p2 = rt2.exec("kill -9 XXX.sh");
 
S

Stefan Schulz

Did you try something like this?

Runtime rt2 = Runtime.getRuntime();
Process p2 = rt2.exec("kill -9 XXX.sh");

Why not use Process.destroy()? I mean, this is what is meant to be used,
and while portability won't be much of an issue if you resort to calling
other programs, it still is a world cleaner then just executing kill.
(especially with SIGKILL. It's meant as a "last resort", not as the
general way to terminate processes)
 

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,777
Messages
2,569,604
Members
45,228
Latest member
MikeMichal

Latest Threads

Top