Question

In my java program I want to kill a process named "DummyBroker"(which is another java program). i could kill it using TaskKill but it needs PID of the process. So how can i fetch the pid for a specific java process and then kill it?

Was it helpful?

Solution 2

Finally found a easy solution here

jps - Java Virtual Machine Process Status Tool , can be used for killing a java process with its name.

for /f "tokens=1" %i in ('C:\java\jdk-7u45-windows-x64\bin\jps -m ^| find "DummyBroker"') do ( taskkill /F /PID %i )

We can add above in batch file and simply call the batch file from java program.

OTHER TIPS

You should take a look at this link : Killing a process using Java

And use something like :

Runtime.getRuntime().exec("taskkill /F /IM <processname>.<extension>")

Otherwise you can maybe use a combinaison of tasklistand split to find the containing line and then find the PID.

Runtime.getRuntime().exec("taskkill /F /IM " + TASKNAME);

Though this only kills by taskname

Go through this link...http://www.golesny.de/p/code/javagetpid

Use java.lang.management.ManagementFactory like this

RuntimeMXBean rtb = ManagementFactory.getRuntimeMXBean();
String processName = rtb.getName();
int result = 0;

Pattern pattern = Pattern.compile("^([0-9]+)@.+$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(processName);
if (matcher.matches()) {
result = new Integer(Integer.parseInt(matcher.group(1)));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top