Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Executing exe using getRuntime.exec() through servlet

 
Sanjaya Sugiarto
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not enough. Read this.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic