check if some exe program is running on the windows

duka.milan picture duka.milan · Sep 25, 2013 · Viewed 42.3k times · Source

How to check if some .exe program is running (is in process) on Windows?

I'm making java application which update one .exe program. So, if that exe program is used by some client, my application ask for closing exe program, and after closing automatically replace .exe file with new one.

Answer

Aneesh picture Aneesh · Sep 25, 2013

You can run the following statement in your java program. Before that you need to know the name of the task in task manager. Say you want to see MS-Word is running. Then run MS-Word, go to task manager and under the process tab, you should see a process named word.exe. Figure out the name for the process you are targeting. Once you have that, you just run the following code:

String line;
String pidInfo ="";

Process p =Runtime.getRuntime().exec(System.getenv("windir") +"\\system32\\"+"tasklist.exe");

BufferedReader input =  new BufferedReader(new InputStreamReader(p.getInputStream()));

while ((line = input.readLine()) != null) {
    pidInfo+=line; 
}

input.close();

if(pidInfo.contains("your process name"))
{
    // do what you want
}