8

I am using java Runtime.exec() method to execute bat file,in bat file i have write code that execute jar.This jar contain thread class that pooling infinite time the rabbitmq queue,if message are found then perform operation on that,mean the process will run infinite. i want to kill this process using java code,also i want know that can this method are able to execute shall 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 to solve the problem.

Sameek Mishra
  • 9,174
  • 31
  • 92
  • 118
  • You cannot run a batch file on Linux, provide a specific version or rewrite the batch in Java! To kill the process take a look to http://stackoverflow.com/questions/6356340/killing-a-process-using-java – Adriano Repetti May 03 '12 at 12:36

3 Answers3

3

Runtime.exec(...) returns Process object which consists 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 passing taskkill /PID <process id> in Runtime.exec(...) or kill -9 <process id>

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
  • 2
    i have try destroy() method but it doesn't close command prompt(process). String myCMD = "cmd.exe /C start c:\\elasticmgmtservice.bat"; Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(myCMD); proc.destroy(); – Sameek Mishra May 03 '12 at 12:56
  • @sam - i have checked 'cmd.exe /C start mspaint.exe' is working fine with destroy(). Fact is there some process those are not responding destroy method. it better you close by 'taskkill' or 'kill'. – Subhrajyoti Majumder May 03 '12 at 13:07
  • @Quoi- when i run the following code with destroy method and 'taskkill' or 'kill' it will not responded, mean it does not kill the process when i try to kill manually from process list it show the java.exe process when i kill this process manually it terminated successfully.i did not understand what happen,if u have any idea please tell me. – Sameek Mishra May 03 '12 at 13:24
  • @sam - wht i think is it could be any other problem. can u please try it - Process p = Runtime.getRuntime().exec("cmd.exe /C start mspaint.exe"); p.destroy(); if it does not work then this is some other problem. cause above code run well in my system. – Subhrajyoti Majumder May 03 '12 at 13:32
  • @Quoi-i tried but when destroy method called the mspaint windows not closed on my PC and what about your side it closed or not? – Sameek Mishra May 03 '12 at 13:39
  • @sam - yes it works in my system. why don't u try in from command prompt or shell. – Subhrajyoti Majumder May 03 '12 at 13:49
1

In Windows

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

In Linux

Runtime rt = Runtime.getRuntime();
rt.exec("kill -9 " +<Your process>);
Diego D
  • 1,735
  • 3
  • 22
  • 38
0

Runtime.exec() returns a Process object, which is an handler for the process the exec method has created. To kill this process, you have to call Process.destroy();

Alexis Dufrenoy
  • 11,784
  • 12
  • 82
  • 124