I want to write a small java app (preferably Java 8) that does the following:
- List all processes that are currently running in Windows 10 and find a specific one (name and PID, there can be multiple running instances of the same app!), e.g. notepad.exe
- Read this process’ command line, e.g. if you use
notepad thisisatest
in command prompt, then it opens a notepad window and the command line saysnotepad thisisatest
in task manager. - If this info contains something specific, then I want to kill the process.
What I’ve already tested for 1. + 2.:
Runtime.getRuntime().exec
as suggested here – it lists the notepad.exe process and its PID but there doesn’t seem to be a way to also request the command line infoProcessHandle
as suggested here (Java 9+, tested with Java 11) – it finds the notepad.exe process but the command line info (process.info().commandLine()
) is empty- jProcesses2 (Java 8, github), the first version was suggested here – this finds the right process but the info (
process.command
) is null
As for killing:
- jProcesses2 provides an example on the github page (
changePriority()
,stop()
, thenkill()
) but that doesn’t work for some reason. Runtime.getRuntime().exec("taskkill /pid 1234 /f")
(I got the PID with jProcesses2) works but I can’t be sure that I’m killing the right process, unless I can also get the info somehow.
- and 3. are working but how do I accomplish 2.?