How to end process using Java?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rawash
    New Member
    • Oct 2009
    • 1

    How to end process using Java?

    I am able to fetch all the processes which are running in Windows task Manager. Now I want to end a particular process from those processes. Is it possible in Java, or do i need to write some native code??
  • myusernotyours
    New Member
    • Nov 2007
    • 188

    #2
    How are you fetching the list in of processes in the first place? Through native code?
    I assume you only want to do it on windows.
    You can use
    Code:
    Runtime.getRuntime().exec("taskkill /IM myprocess.exe");
    Regards,

    Alex.

    Comment

    • swatantra
      New Member
      • Jul 2015
      • 1

      #3
      You can kill the process from task manager using Java

      Code:
      Runtime.getRuntime().exec("taskkill/F/IM processName.exe");
      you can download the jar file to kill the process of task manager and you can see the java code for this task.

      Comment

      • Kara Hewett
        New Member
        • Apr 2014
        • 27

        #4
        You can also use Task Scheduler on a Windows Server and kill any java program which is running on that server.

        Comment

        • Sherin
          New Member
          • Jan 2020
          • 77

          #5
          Code:
          public class ProcessDemo  
              public static void main(String[] args)  
              { 
                  try 
                  { 
                        
                      // create a new process 
                      System.out.println("Creating Process"); 
                        
                      ProcessBuilder builder = new ProcessBuilder("notepad.exe"); 
                      Process pro = builder.start(); 
                
                      // wait 10 seconds 
                      System.out.println("Waiting"); 
                      Thread.sleep(10000); 
                
                      // kill the process 
                      pro.destroy(); 
                      System.out.println("Process destroyed"); 
                
                  }  
                      catch (Exception ex)  
                  { 
                      ex.printStackTrace(); 
                  } 
              } 
          }
          Output:

          Creating Process
          Waiting
          Process destroyed

          Comment

          Working...