Good afternoon, how can you programmatically check whether a particular application is running, Windows?
|
2 answers
public boolean checkPPOpen(String name) throws IOException { String line; final Process process = Runtime.getRuntime().exec("tasklist.exe"); final BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); while ((line = reader.readLine()) != null) { if (!line.contains(name)) { continue; } return true; } reader.close(); return false; } - False reaction to test.exe while maintest.exe is running - GreyGoblin
- 2and when we find a match reader is not necessary to close? - Artem Konovalov
- @ArtemKonovalov in this situation is best wrapped in try-with-resources - I. Perevoz
- @ I.Perevoz I know about it, it was a subtle hint to the author that he had an incorrect code - Artem Konovalov
- @ArtemKonovalov this resource seems to be not for subtle hints. If there is something to advise, stick your nose where necessary. - I. Perevoz
|
Adaptation of the answer: https://stackoverflow.com/a/54950/3347171/
import java.io.*; public class Test { public static void main(String[] s) { try { String line; Process p = Runtime.getRuntime().exec("tasklist.exe"); BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); while ((line = input.readLine()) != null) { // Тут будет строка вида: // tasklist.exe 1234 Console 1 414я444 ?? // то есть то, что выводит tasklist.exe в консоли. // Парсите строку и вытягиваете то, что вам нужно. System.out.println(line); } input.close(); } catch (Exception err) { err.printStackTrace(); } } } - Specify the source from where you took the material - this is the first. Second, the vehicle asked for a check on a specific process, but not a list at all - GenCloud
- @GenCloud link to the source added. On the second - already a niggle. - GreyGoblin
- why de niggle, just a statement of fact - GenCloud
- I think @GenCloud in a site format is important to give an idea of a solution, and not a complete application. For example, from the condition of the task it is not clear in what form the information will be received. That's why I brought the solution in this form, and left the implementation of parsing to choose the vehicle. If you really appreciate it, then your answer will give an erroneous result in some cases. That's why I think the second part of your comment is a niggle. - GreyGoblin
- one@VargSieg take a look at the task as a whole. Maybe the crutch is the need to monitor the state of the process in the system and you can somehow refuse it. - GreyGoblin
|