3

On Windows 7, I have a Java Application, which can start other JAR-s (which are running in background).

In this Application, I have a button, which shoul represent the CTRL+C command for every other started (and still running) JAR-s. I have found this code, which should kill the task by PID (6272):

taskkill /F /PID 6272

But if I run this code on a command propmt, a lot of times it is just waiting, and than return false, so it cannot kill the program. Sometimes it can kill, sometimes it cannot.

I would need a command, which is similar to CTRL+C, because if you hit this CTRL+C, the actual process will end immediately, without waiting!

Could anyone help me please? Thank you!

victorio
  • 6,224
  • 24
  • 77
  • 113
  • you need java code or command line, and how you are launching jars. Is it by `ProcessBuilder` or `Runtime.exec` – Deepak Bhatia Jan 18 '16 at 11:29
  • I am using this code right now in my java app: **Process p = Runtime.getRuntime().exec("taskkill /F /PID 6272");** But I need a command, which I can use in this code, and also in CMD by myself – victorio Jan 18 '16 at 11:41
  • How you launch the java process???? – Deepak Bhatia Jan 18 '16 at 11:46
  • **Process process = Runtime.getRuntime().exec("cmd /c java -Xmx100M -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=false -jar myapp.jar"); process.waitFor();** and this waitFor method usually return 128 (0 is the return, if i could kill the task) – victorio Jan 18 '16 at 12:01
  • 1
    Have tried adding `/T` option? – TT. Jan 18 '16 at 14:17
  • what is the /T option? – victorio Jan 19 '16 at 09:11
  • 1
    @victorio /T option Specifies to terminate all child processes along with the parent process. More info at https://technet.microsoft.com/en-in/library/bb491009.aspx – Deepak Bhatia Jan 19 '16 at 09:37

1 Answers1

1

First of all why you need the command to fire from Java when you have the hold of Process object by which you launch a jar, you can simply use destroy(); to kill the process you have launched.

Process p = Runtime.getRuntime().exec( "bla bla bla");      
p.destroy();

And if you really need to do it manually from command prompt then you have to find the process id and kill it by using the command you have already mentioned, you can read more here kill process command line windows 7 and Microsoft Taskkill

Deepak Bhatia
  • 6,230
  • 2
  • 24
  • 58