Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close running process using java?

Tags:

java

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.

like image 363
Sameek Mishra Avatar asked May 03 '12 12:05

Sameek Mishra


1 Answers

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>

like image 171
Subhrajyoti Majumder Avatar answered Oct 23 '22 05:10

Subhrajyoti Majumder