How to Find and Kill Running Win-Processes from Within Java

How to find and kill running Win-Processes from within Java?

You can use command line windows tools tasklist and taskkill and call them from Java using Runtime.exec().

How to find the process id of a running Java process on Windows? And how to kill the process alone?

You can use the jps utility that is included in the JDK to find the process id of a Java process. The output will show you the name of the executable JAR file or the name of the main class.

Then use the Windows task manager to terminate the process. If you want to do it on the command line, use

TASKKILL /PID %PID%

Find and kill a specific Java process from another Java App

It's probably going to be a lot simpler using OS-specific tools and using Runtime.exec() to run them, but I'll try and give a platform independent answer:


It might be possible to do this platform independently using the Attach API. This comes with the JDK, so to use it just include tools.jar from your JDK on your program's classpath.

To get a list of virtual machines on the system, use VirtualMachine.list(). You can get/parse arguments from the virtual machine descriptor objects that are returned from this.

The attach API also allows you to load agents into already-running Java processes. Since you want to kill a Java process, you can write a Java agent that simply runs System.exit() (or if you really want it dead use Runtime.halt() instead) when the agent loads.

Once you identify the one you want to kill, attach to it and load the killer agent (the agent has to be built as a JAR file, accessible to the Java process it needs to be loaded into). Shortly after the agent is attached that process should die.

These links might help also:

An Oracle blog on the attach API

Package documentation for java.lang.instrument (has detailed instructions on how to build an agent JAR)

How to find and close running Win-Process which is Java app from within other Java app?

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.

How can we stop a running java process through Windows cmd?

It is rather messy but you need to do something like the following:

START "do something window" dir
FOR /F "tokens=2" %I in ('TASKLIST /NH /FI "WINDOWTITLE eq do something window"' ) DO SET PID=%I
ECHO %PID%
TASKKILL /PID %PID%

Found this on this page. (archived)

(This kind of thing is much easier if you have a UNIX / LINUX system ... or if you run Cygwin or similar on Windows.)

Knowing which java.exe process to kill on a windows machine

Download Sysinternal's Process Explorer. It's a task manager much more powerfull than Windows's own manager.

One of it's features is that you can see all the resources that each process is using (like registry keys, hard disk directories, named pipes, etc). So, browsing the resources that each java.exe process holds might help you determine wich one you want to kill. I usually find out by looking for the one that's using a certain log file directory.

Kill a java process in windows programmatically

for /f "delims= " %%a in ('jps -ml ^| find /i "common"') do set PID=%%a
taskkill /f /PID %PID%

or from command prompt:

for /f "delims= " %a in ('jps -ml ^| find /i "common"') do set PID=%a


Related Topics



Leave a reply



Submit