3

There is windows process which is actually a java application (java.exe). I need to close it from within other java application (other running .jar) which is probably will also be named as java.exe in windows taskbar.

To do the task we would normally do it this way

Runtime.getRuntime().exec(KILL + serviceName); // where serviceName is a `java.exe`

But there would be some java.exe processes in windows taskbar. So how to find the one I need? In this case it is forum.jar.

Actually this jar was started from .bat file (if it makes any difference) - it is process.bat as it seen in the screenshot.

enter image description here

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
rozerro
  • 5,787
  • 9
  • 46
  • 94
  • 1
    I believe this will help you: [How to get a list of current open windows/process with Java?](https://stackoverflow.com/questions/54686/how-to-get-a-list-of-current-open-windows-process-with-java) Alternatively, try [jps](https://docs.oracle.com/en/java/javase/17/docs/specs/man/jps.html). – Abra Aug 17 '22 at 10:27

1 Answers1

0

You could run

wmic process where caption="java.exe" get commandline,description,processid

to get the list of processes and their associated command line like in your screenshot. Then you get the ID of the command you want to kill and kill it with taskkill /pid xxx.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • How did you get command line string from tasklist output? I could get process nane, pid, session name, memory and session number. But to detect the pid which is related to `forum.jar` there should be a way to obtain the command line string from there. – rozerro Aug 17 '22 at 13:49
  • My bad it does not work, see my edit. – assylias Aug 17 '22 at 14:09