File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Process.destroy is not working on process invoked by sub-thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Process.destroy is not working on process invoked by sub-thread" Watch "Process.destroy is not working on process invoked by sub-thread" New topic

Process.destroy is not working on process invoked by sub-thread

Lakshya Bhardwaj

Joined: Nov 28, 2012
Posts: 1

Sory I am doing Java code after many many years so might be doing something wrong and need help in resolving this issue.

My main thread (or main program) is reading a file and for each entry in file it is invoking a sub-thread that perform series of action like doing ping etc.

In each child thread I am invoking process to execute certain tasks or execute batch file to make connection to remote server and perform activity.

There are certain server where commands\tasks are not returning and hence compelete program goes into hung state. I want to kill those process that triggered those commands\tasks and trying to use process.destroy. But tt is not working at all.

Please suggest what could be the issue as I have tried almost everything that I can think of and how to fix it. Thank a ton in advance.

public static void main(String [] args) throws InterruptedException


Thread thread = new OQ(IP,UserName,Pass,Port,FQDN,OS,dateNow,LoggedInUser,"","mc");

public void run()

cmnd = "agent_check.bat " + UserName + " " + Pass + " " + IP + " " + Port + " 40";

Full_Dump += cmnd+"\n";

// cmnd="cmd.exe /C patrolcli \"user " + UserName + " " + Pass + "\" \"connect " + IP + " " + Port + "\" \"execpsl set(\\\"/AS_EVENTSPRING/EVENTSPRING/AlertTest/value\\\",40);\""; i also tried executing command directly instead of wrapping it in batch file

BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));

while( (cmdOutput=reader.readLine()) != null )
Full_Dump += cmdOutput+"\n";
Output += cmdOutput+"\n";

System.out.println("reading done");

} catch (InterruptedException ie)

System.out.println("killing process");
Full_Dump +="Interrupted while waiting for process; time likely ran out. Destroying process...";


William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
But tt is not working at all.

How far does it actually get?

Suppose the output is never terminated with a newline character? (Thats why I just read character by character from the process output)

Richard Tookey

Joined: Aug 27, 2012
Posts: 1166

There are two obvious things wrong with your code fragments. First, though you call p.waitFor() you totally ignore the return code which may or may not contain diagnostic information and second you ignore the process 'stderr'! The most common 'hang up' scenario I have met when using Runtime.exec() is caused by failure to handle 'stderr' properly when the process fails. In this situation one can get a deadlock when the 'stderr' buffer becomes full and the process suspends until more buffer space is available to write more of 'stderr'. If the Java side does not read the 'stderr' stream the buffer never ever ever has space available - a deadlock!

All this and more is detailed in the ancient but still very relevant article - . You should read this and implement all the recommendations. Don't be tempted to miss out anything because you think it cannot apply to you because it can. Note - pay particular attention to the use of separate threads for handling the various Process streams.
It is sorta covered in the JavaRanch Style Guide.
subject: Process.destroy is not working on process invoked by sub-thread
It's not a secret anymore!