15

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.

Denim Datta
  • 3,528
  • 3
  • 25
  • 53
duka.milan
  • 171
  • 1
  • 2
  • 7
  • 1
    check this question: [How to get a list of current open windows/process with Java?](http://stackoverflow.com/questions/54686/how-to-get-a-list-of-current-open-windows-process-with-java) – Geeky Guy Sep 25 '13 at 12:51
  • 1
    Questions about general computing are off-topic on Stack Overflow. I suggest you try your luck on [Super User](http://superuser.com) –  Sep 25 '13 at 12:52

4 Answers4

37

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
}
Manjula
  • 4,494
  • 3
  • 25
  • 36
Aneesh
  • 1,651
  • 2
  • 20
  • 33
  • 1
    Rather than concatenate all of the lines together, it would probably be much more efficient to check for `line.contains(processName)` inside the `while` loop. – 4castle Oct 01 '16 at 20:52
  • Checking each line if it contains `(processName)` will take more time then checking in `pidInfo` after `while ` loop. – pavlo Nov 30 '17 at 18:46
  • 1
    Tasklist returns all running exe. Is it possible to get only the list of exe which are running in the foreground? – Aman Jun 07 '19 at 10:13
3

You can try running the following code :

Runtime rt = Runtime.getRuntime();

and execute "tasklist"

tasklist returns a list of currently executing processes (as shown in the task manager's process tab).

svarog
  • 8,471
  • 4
  • 55
  • 66
TheLostMind
  • 34,842
  • 11
  • 64
  • 97
3

Here's a full code for checking if an application is running on a Windows system or not:

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

public class ApplicationUtilities
{
    public static void runApplication(String applicationFilePath) throws IOException, InterruptedException
    {
        File application = new File(applicationFilePath);
        String applicationName = application.getName();

        if (!isProcessRunning(applicationName))
        {
            Desktop.getDesktop().open(application);
        }
    }

    // http://stackoverflow.com/a/19005828/3764804
    private static boolean isProcessRunning(String processName) throws IOException, InterruptedException
    {
        ProcessBuilder processBuilder = new ProcessBuilder("tasklist.exe");
        Process process = processBuilder.start();
        String tasksList = toString(process.getInputStream());

        return tasksList.contains(processName);
    }

    // http://stackoverflow.com/a/5445161/3764804
    private static String toString(InputStream inputStream)
    {
        Scanner scanner = new Scanner(inputStream, "UTF-8").useDelimiter("\\A");
        String string = scanner.hasNext() ? scanner.next() : "";
        scanner.close();

        return string;
    }
}

For instance, you could use the runApplication() method to only run the application when it is not running yet:

ApplicationUtilities.runApplication("C:\\Program Files (x86)\\WinSCP\\WinSCP.exe");

The same principle applies for deleting the executable.

BullyWiiPlaza
  • 12,477
  • 7
  • 82
  • 129
1

Just a suggestion for users of Java 9 or higher.
It's even operating system independent:

Interface ProcessHandle
static Stream<ProcessHandle>    allProcesses​()

More details at:
https://docs.oracle.com/javase/9/docs/api/java/lang/ProcessHandle.html