How to close a running process using java?

I use the java Runtime.exec () method to run the bat file, in the bat file I have a write code that executes jar.This jar contains a stream class that combines the endless time of the rabbitmq queue, if the message is found, then do the operation, this means that the process will be endless. I want to kill this process with java code, also I want to know if this method can execute a script on Linux Os.

**Used java code** String myCMD = "cmd.exe /C start c:\\elasticmgmtservice.bat"; Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(myCMD); **used batch file** cd c: cd ElasticMgmtService\ java -jar ElasticIndexManagementService.jar config\ElasticIndexManagementService.xml 

please help me solve the problem.

+6
source share
3 answers

Runtime.exec (...) returns a Process object, which consists of the following methods

  • destroy ()
  • exitValue ()
  • getErrorStream ()
  • getInputStream ()
  • getOutputStream ()
  • WAITFOR ()

you can call destroy (), which kills the subprocess. The subprocess represented by this Process object is forcibly terminated. or you can kill by going to taskkill /PID <process id> in Runtime.exec(...) or kill -9 <process id>

+2
source

In windows

 Runtime rt = Runtime.getRuntime(); rt.exec("taskkill " +<Your process>); 

On linux

 Runtime rt = Runtime.getRuntime(); rt.exec("kill -9 " +<Your process>); 
+2
source

Runtime.exec() returns a Process object, which is the handler of the process created by the exec method. To kill this process, you must call Process.destroy();

0
source

Source: https://habr.com/ru/post/914805/


All Articles