-1

Is there a way to stop a running process using java? I'm using this piece of code to run a command:

Process process = Runtime.getRuntime().exec(String.format("cmd /c %s", command));

Afterwards, I need to end/stop/terminate it.

Does anyone knows how to do this?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
lrente
  • 1,070
  • 1
  • 9
  • 27
  • What happened when you ran this? – Shark Jul 22 '13 at 17:03
  • As a general tip: Read (and implement) *all* the recommendations of [When Runtime.exec() won't](http://www.javaworld.com/jw-12-2000/jw-1229-traps.html). Then ignore that it refers to `exec` and build the `Process` using a `ProcessBuilder`. Also break a `String arg` into `String[] args` to account for arguments which themselves contain spaces. – Andrew Thompson Jul 22 '13 at 17:13

1 Answers1

2

There is a destroy() method in Process, which can be of use.

public abstract void destroy()

Kills the subprocess. The subprocess represented by this Process object is forcibly terminated.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136