Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if some exe program is running on the windows

Tags:

java

process

exe

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.

like image 620
duka.milan Avatar asked Sep 25 '13 12:09

duka.milan


4 Answers

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
}
like image 143
Aneesh Avatar answered Nov 15 '22 14:11

Aneesh


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.

like image 29
BullyWiiPlaza Avatar answered Nov 15 '22 15:11

BullyWiiPlaza


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).

like image 36
TheLostMind Avatar answered Nov 15 '22 15:11

TheLostMind


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

like image 30
Mr. Michael Eder Avatar answered Nov 15 '22 13:11

Mr. Michael Eder