Results 1 to 3 of 3
  1. #1
    hahanizhu is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default How to destroy a Process in windows?

    I would like to run a Process and get its output. But this process may hang sometimes. So I need to set a timeout for this Process. But I found that Process.destroy() didn't really kill the hanging process. Could anyone help on this?


    import java.util.*;
    import java.io.*;
    class StreamGobbler extends Thread
    {
    InputStream is;
    String type;

    StreamGobbler(InputStream is, String type)
    {
    this.is = is;
    this.type = type;
    }

    public void run()
    {
    try
    {
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line=null;
    while ( (line = br.readLine()) != null)
    System.out.println(type + ">" + line);
    } catch (IOException ioe)
    {
    ioe.printStackTrace();
    }
    }
    }
    public class test
    {
    public static void main(String args[]) throws InterruptedException
    {
    Runnable r = new ShengThread();
    Thread sheng = new Thread(r);
    sheng.start();
    sheng.join(1000);
    sheng.interrupt();



    }
    }

    class ShengThread implements Runnable {
    public void run() {
    Process proc = null;
    try
    {
    String osName = System.getProperty("os.name" );
    String[] cmd = new String[3];
    if( osName.equals( "Windows XP" ) )
    {
    cmd[0] = "cmd.exe" ;
    cmd[1] = "/C" ;
    cmd[2] = "c:\\status.bat";
    }


    Runtime rt = Runtime.getRuntime();

    System.out.println(osName+"Execing " + cmd[0] + " " + cmd[1]
    + " " + cmd[2]);
    try {
    proc = rt.exec(cmd);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    // any error message?
    StreamGobbler errorGobbler = new
    StreamGobbler(proc.getErrorStream(), "ERROR");

    // any output?
    StreamGobbler outputGobbler = new
    StreamGobbler(proc.getInputStream(), "OUTPUT");

    // kick them off
    errorGobbler.start();
    outputGobbler.start();

    // any error???
    int exitVal = proc.waitFor();
    System.out.println("ExitValue: " + exitVal);
    } catch (InterruptedException t)
    {
    System.out.println("start\n");
    proc.destroy();
    t.printStackTrace();
    }

    }
    }

    After 1 sec timeout, the process is not destroyed. I have to use ctrl+c to kill the process.

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    If 'destroy' doesn't work, you can try exiting your java process, which *usually* kills any sub-processes. But if the subprocess remains even after the java process has exited, then you're out of luck from what Java can do, and will have to try to figure out why the child process is hanging, or launch the subprocess with a wrapper script that can issue more extreme OS-specific kill commands, or find a way to avoid launching the buggy subprocess altogether.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Thumbs up

    if you want to interrupt/stop a thead you can use such a structure inside run()

    Java Code:
     public void run() {
               while(true) {
                  if (isInterrupted() {
                  // cleanup and break
                  break;
                 }
                 // code to run
               }
              try {} catch(InterruptedException ie) {
                             interrupt();
              }
    }

    when interrupt() is called the code inside the if-block is executed and the while-loop is broken and the thread finish.
    Last edited by j2me64; 04-26-2011 at 10:25 AM.

Similar Threads

  1. Jbutton to destroy process
    By peterhammond in forum AWT / Swing
    Replies: 1
    Last Post: 04-19-2011, 06:58 PM
  2. Destroy object
    By anjanesh in forum New To Java
    Replies: 11
    Last Post: 01-02-2010, 07:25 PM
  3. Replies: 11
    Last Post: 02-13-2009, 12:52 PM
  4. Behaviour of stop() and destroy()-methods
    By Zamppa in forum Java Applets
    Replies: 0
    Last Post: 02-10-2009, 09:50 AM
  5. Replies: 2
    Last Post: 01-06-2009, 03:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •