title: How to close a external application with java
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/62418/knowing-which-java-exe-process-to-kill-on-a-windows-machine">Knowing which java.exe process to kill on a windows machine</a><br>
    <a href="https://stackoverflow.com/questions/4633678/how-to-kill-a-process-in-java-given-a-specific-pid">How to kill a process in Java, given a specific PID</a>  </p>
  </blockquote>
  
  
  
  <p>I am trying to find out how to close a particular external exe namely cwserv5.exe. I have succeeded in starting a new external exe and closing it .But not an existing process. Can you help? Below is what I was tinkering with but really lost to be honest</p>
  
  <pre><code>package com.TestCase;
  
  import java.io.BufferedReader;
  import java.io.InputStreamReader;
  
  public class ReStartEXE {
  
      static Process pr; 
  
      public static void open() {
  
          //ProcessBuilder
  
           try {
               Runtime rt = Runtime.getRuntime();
               //Process pr = rt.exec("cmd /c dir");
                pr = rt.exec("C:\\APPLEGREEN\\webserv\\cwserv5rost.exe");         
  
               Thread.sleep(10000);
               //pr.wait(10000);
              //pr.waitFor();
  
               BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
  
              // String line=null;
  
               /*while((line=input.readLine()) != null) {
                   System.out.println(line);
               }*/
  
               //int exitVal = pr.waitFor();
               //pr.destroy();
             // Process.kill(pr);
              // Runtime.getRuntime().exec("taskkill /F /IM cwserv5rost.exe");
  
               //System.out.println("Exited with error code "+exitVal);
  
           } catch(Exception e) {
               System.out.println(e.toString());
               e.printStackTrace();
           }
       }
  
  
      public static void Close() {
  
          pr.destroy();
  
      }
  
  }
  </code></pre>