I have a problem. I need to execute an exe. I have tried with a simple main method and it works like a charm. But as I try to integrate into Tomcat, it hangs. The program uses pdftohtml to convert a pdf into html.
Here is the code:
It hangs just before the waitFor(). Have you guys any idea how to solve this? Many thanks.
PS: I tried this with tomcat bundled with Netbeans 5.
Sounds like a very familiar problem - when you exec an external program, the Process takes over the standard input, output and error streams. If you do not provide for consuming these streams, the program probably hangs indefinately because it is trying to write to the output and/or error stream.
This is all covered in the java.lang.Process JavaDocs discussion.
Joined: Mar 25, 2004
Sorry for my ignorant, but how can I provide to consume those streams? In Javadoc there is no detail how to do that.
Author and all-around good cowpoke
Joined: Mar 22, 2000
Is adding those 3 lines before the waitFor should be enough? Or you meant something different?
Just obtaining the output streams is not enough, you have to provide for reading from them - just like it says in the JavaDocs for Process.
Note that if your exe is producing an error, it will likely be reported as messages to either stderr or stdout. You will need a separate Thread to read each stream and write the result somewhere so you can check it later.
You would only need to write to the stdin stream if your program requires keyboard input.