my dog learned polymorphism*
The moose likes Servlets and the fly likes Executing exe using getRuntime.exec() through servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Executing exe using getRuntime.exec() through servlet" Watch "Executing exe using getRuntime.exec() through servlet" New topic
Author

Executing exe using getRuntime.exec() through servlet

Sanjaya Sugiarto
Ranch Hand

Joined: Mar 25, 2004
Posts: 229
Hello everyone,

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.


<a href="http://www.wi.hs-furtwangen.de" target="_blank" rel="nofollow">Business Information Technology - Hochschule Furtwangen University, Germany</a>
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    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.

Bill
Sanjaya Sugiarto
Ranch Hand

Joined: Mar 25, 2004
Posts: 229
Sorry for my ignorant, but how can I provide to consume those streams? In Javadoc there is no detail how to do that.

Thanks.
Sanjaya Sugiarto
Ranch Hand

Joined: Mar 25, 2004
Posts: 229
Hmmm....you meant:

process.getErrorStream();
process.getInputStream();
process.getOutputStream();

Is adding those 3 lines before the waitFor should be enough? Or you meant something different?

Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Not enough. Read this.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
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.

Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Executing exe using getRuntime.exec() through servlet
 
Similar Threads
Killing process spawned by Runtime.exec()
this code runs in TextPad or Kawa but doesn't run in dos promt..!!!!!
Executing exe using getRuntime.exec() through servlet
wait runtime object finish execution
Process waits forever when using Stringbuffer with more than 2000 strings