Runtime exec and custom RTF editor
I have a class that controls the generation of RTF documents and a method in that class that calls the RTF editor with the XML file to display.
All but one user can access this editor without any problem. This one user is constantly running into an issue where their application just freezes. There are no errors in any logs. Usually this problem is easily identified, reproducible and fixed, however I cannot reproduce it for the rest of my life without my attempts to debug.
Basically the code looks like this:
int exitVal = CUBSRTFEditor.runRTFEditor("c:\\tmp\\control"+ap_doc_id+".xml", xml,"I:\\AppealsLetters.exe /process \"c:\\tmp\\control"+ap_doc_id+".xml\"");
public static int runRTFEditor(String xmlLocation, String xmlContent, String executePath)
{
int exitVal = 0;
createLocalFile(xmlLocation, xmlContent);
try
{
System.out.println("executePath must = "+executePath);
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(executePath);
System.out.println("after executePath runs");
//exhaust that stream before waiting for the process to exit
InputStream inputstream = proc.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
// read the ls output
String line;
while ((line = bufferedreader.readLine())!= null)
{
System.out.println(line);
}
exitVal = proc.waitFor();
}
catch (Throwable t)
{
t.printStackTrace();
}
CUBSRTFEditor.deleteTempFile(xmlLocation);
return exitVal;
}
The last output is the first System.out. When I take the xml file and execute it on any other PC, it runs without issue. I don't see any useful information in proc.getErrorStream () or proc.getOutputStream ().
JDK Javadoc documentation on this issue (execution check): Since some native frameworks provide limited buffer sizes for standard input and output streams, not being able to quickly write an input stream or read a subprocess output stream can result in subprocess blocking and even deadlock.
I am trying to exhaust this thread before waiting for the process to exit and it doesn't seem to help as it doesn't seem to reach that point (the second System.out is not displayed)
Did I do it wrong? Am I missing something important? Any ideas on how to get more information out of the process would be great.
I am stuck....
a source to share
Runtime.exec () is a deceptively nasty little problem to work with. I found this article (old but current) to be quite helpful. You can always skip the page for some highly gankable code. :-)
At first glance, your code should be handling both proc.getOutputStream () and proc.getErrorStream (), which is a good reason to handle these streams on separate threads.
a source to share
I wanted to update this because this change moved to Production today and it worked. Based on suggestions from BlairHippo, I got it to work with an anonymous inner class to create a separate thread to exhaust the error and input streams.
new Thread(new Runnable(){
public void run()
{
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
String line;
while ((line = br.readLine())!= null)
{
System.out.println(line);
}
}
catch (Throwable t)
{
t.printStackTrace();
}
}
}).start();
a source to share