Programmatically close jar

Asked

Viewed 368 times

2

Currently I have a program JAVA(main), which from it calls another program JAVA (son)... this is OK.. but when I close the main program, I also want to close the son program.

I’ve tried to:

Runtime.getRuntime().exec("taskkill /F /PID **NOMEDOPROGRAMA**");

But it didn’t work...

  • The taskkill /PID awaits the pid of the program, not its name. You could use /IM to pass the name, but the problem is that the name of the program in the case of java programs is always java.exe (or javaw.exe). You will end up killing the son and the father together. How are you doing to launch the son process?

  • Ah I managed to solve... I was calling the jar by the Runtime itself!

1 answer

4


When creating the process with the method exec() an instance of Process. Then you can store this reference and destroy it later with the method destroy().

Example:

Process p = Runtime.getRuntime().exec("**NOMEDOPROGRAMA**");
...
p.destroy();
  • Thank you! I hadn’t paid attention! hehehe

Browser other questions tagged

You are not signed in. Login or sign up in order to post.