How to stop an external process in a Java Program

Discussion in 'Java' started by Jerome, Jan 21, 2005.

  1. Jerome

    Jerome Guest

    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
    Jerome, Jan 21, 2005
    #1
    1. Advertising

  2. On Fri, 21 Jan 2005 11:16:21 +0000, Jerome wrote:

    > 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.

    --
    In pioneer days they used oxen for heavy pulling, and when one ox
    couldn't budge a log, they didn't try to grow a larger ox. We shouldn't
    be trying for bigger computers, but for more systems of computers.
    --- Rear Admiral Grace Murray Hopper
    Stefan Schulz, Jan 21, 2005
    #2
    1. Advertising

  3. Jerome

    Ann Guest

    "Stefan Schulz" <> wrote in message
    news:p...
    > On Fri, 21 Jan 2005 11:16:21 +0000, Jerome wrote:
    >
    > > 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.


    Did you try something like this?

    Runtime rt2 = Runtime.getRuntime();
    Process p2 = rt2.exec("kill -9 XXX.sh");
    Ann, Jan 21, 2005
    #3
  4. On Fri, 21 Jan 2005 17:06:14 -0600, Ann wrote:

    >> > I have:
    >> > ...
    >> > Runtime rt1 = Runtime.getRuntime();
    >> > Process p1 = rt1.exec("XXX.sh");
    >> >
    >> > ...
    >> >
    >> > Does anyone know how to kill a process?
    >> > Thanks in advance.

    >
    > 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)

    --
    In pioneer days they used oxen for heavy pulling, and when one ox
    couldn't budge a log, they didn't try to grow a larger ox. We shouldn't
    be trying for bigger computers, but for more systems of computers.
    --- Rear Admiral Grace Murray Hopper
    Stefan Schulz, Jan 22, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Theule

    Stop Debugging doesn't stop in ASP.NET

    Matt Theule, Jul 23, 2003, in forum: ASP .Net
    Replies:
    7
    Views:
    713
    Matt Theule
    Jul 24, 2003
  2. Yadagiri Rao KP
    Replies:
    1
    Views:
    8,921
    Moshe Sayag
    Aug 10, 2003
  3. Will
    Replies:
    1
    Views:
    15,178
    Thomas Weidenfeller
    Nov 2, 2004
  4. Earl Eiland
    Replies:
    0
    Views:
    320
    Earl Eiland
    Mar 18, 2005
  5. Earl Eiland
    Replies:
    0
    Views:
    362
    Earl Eiland
    Mar 19, 2005
Loading...

Share This Page