Tuesday, December 14, 2010

Find and Kill Running Windows Processes in Java

One of my friends asked me if it is possible to check a programming is running or not in Windows using Java. And close it if it is running. After a little bit research, I found the Java solution for his requirement.

The key is Runtime.getRuntime().exec(), you can run commands available in this platform. In Windows, the "tasklist" command will show you all the processes running (In Linux, you might want to look at "ps").
tasklist

The result of this command looks like this:

Image Name             PID Session Name Session#    Mem Usage
========================== ===================== ============
System Idle Process      0 Services            0         24 K
System                   4 Services            0      1,260 K
svchost.exe            896 Services            0      7,060 K
taskmgr.exe            868 Console             2     13,300 K
WINWORD.EXE           5412 Console             2     39,860 K
iexplore.exe          5256 Console             2     69,104 K
eclipse.exe           1112 Console             2      1,788 K
javaw.exe             4380 Console             2    555,552 K
cmd.exe               1500 Console             2      3,264 K
conhost.exe           3120 Console             2      7,188 K
bash.exe              3360 Console             2      7,840 K
sh.exe                3412 Console             2      7,456 K
java.exe               824 Console             2    317,116 K
Acrobat.exe           5484 Console             2     49,784 K

Here is the code to iterate and find running process:

 private static final String TASKLIST = "tasklist";

 public static boolean isProcessRunging(String serviceName) throws Exception {

  Process p = Runtime.getRuntime().exec(TASKLIST);
  BufferedReader reader = new BufferedReader(new InputStreamReader(
    p.getInputStream()));
  String line;
  while ((line = reader.readLine()) != null) {

   System.out.println(line);
   if (line.contains(serviceName)) {
    return true;
   }
  }

  return false;

 }

When you make sure the enemy process is runing, you can use "taskkill /IM xxx.exe" to kill it. For example, let's kill MS Word:

taskkill -IM WINWORD.EXE
Below is the code to kill process by name:
 private static final String KILL = "taskkill /IM ";
public static void killProcess(String serviceName) throws Exception {

  Runtime.getRuntime().exec(KILL + serviceName);

 }

Let's have a look how to use this process helper:
 public static void main(String args[]) throws Exception {
  String processName = "WINWORD.EXE";
  
  //System.out.print(isProcessRunging(processName));

  if (isProcessRunging(processName)) {

   killProcess(processName);
  }
 }

10 comments:

Vinod said...

Great Work Buddy Really appreciate this .... helped me saving lots of time .. cheers

warm regards
Vinod M

Patri said...

Thank you so much, Ke Cai!!!
It is working perfectly, and it saved me a lot of time.

Regards,
Patricia

Maneesh said...

Thank you for so much of your help....Ke Cai!!!!!
It helped me alot for our project

Paddy said...

thanks for giving code I will try it on my system for my project.

thanks alot Cai.

Etaytch said...

Thanks A lot!!

pronicles said...

Thanks! Very helpfull article!

omasampath said...

hi,

I want kill exe name with space. but couldn't do that. Please give a solution.
exe name - "Mobile Partner.exe"

thanks
sampath

Diego said...

Thanks a lot for your help!

bora.pradnya said...

Thanks

Luís Assunção said...

Unfortunatly this solution don´t work if you have many Java application running, because you have many java.exe images!!