check if some exe program is running on the windows

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


check if some exe program is running on the windows



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.





check this question: How to get a list of current open windows/process with Java?
– Renan
Sep 25 '13 at 12:51







Questions about general computing are off-topic on Stack Overflow. I suggest you try your luck on Super User
– user2109908
Sep 25 '13 at 12:52




4 Answers
4



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:


task manager


word.exe


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
}





+1 for the full code :P
– TheLostMind
Sep 25 '13 at 13:18





Thanks Tharindu Rusira that was help me to resolve problem!
– duka.milan
Dec 17 '13 at 11:17





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 1 '16 at 20:52


line.contains(processName)


while





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


(processName)


pidInfo


while



U can try the following :



Runtime rt = Runtime.getRuntime();
and execute "tasklist"



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





Thanks TheLostMind that is very simple approach.
– duka.milan
Dec 17 '13 at 11:19



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


Windows


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:


runApplication()


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



The same principle applies for deleting the executable.



Just a suggestion for Java 9 or higher:


Interface ProcessHandle
static Stream<ProcessHandle> allProcesses​()



See also:
https://docs.oracle.com/javase/9/docs/api/java/lang/ProcessHandle.html






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived