1

I am trying to run an external program Decoder.exe using a java: Runtime.getRuntime().exec("C:\fullpath-and-so-on\Decoder.exe -h"); like so:

    try{
        Process p = Runtime.getRuntime().exec("C:\\fullpath-and-so-on\\Decoder.exe -h");
    }
    catch(Exception e){
        e.printStackTrace();
    }

This works for all other programs I've tried. But whenever I execute it from java the Decoder.exe crashes. The java executes fine and does not generate any exceptions but the called program Decodes.exe stops working. The program Decoder.exe executes perfectly when run in a separate cmd window.

Have anyone had any experience with a similar problem? What differs when java runs exec and when a program is run in cmd.exe? Is there a known workaround or am I just making a mistake somewhere?

Very thankful for any help! BR, Fredrik

liket
  • 193
  • 1
  • 8
  • Do you have any lead for what error happens? The execution environment might be slightly different like the folder the program runs in. – marsbear May 29 '11 at 15:37
  • No leads what so ever. Since it's the called program that stops working I get no stacktrace. Since using the full path to the Decoder.exe I figured the current path wouldn't matter(?) – liket May 29 '11 at 16:14

2 Answers2

2

Stops working you say?

Is the decoder.exe writing output to stderr or stdout? You must in that case read those streams, since the buffers for the streams are very small, and the execution will halt if those buffers get full.

This is a great article, it's old, but it still holds: When Runtime.exec() won't

Kaj
  • 10,862
  • 2
  • 33
  • 27
  • Thank you very much for the suggestion. However I do not think it is the buffers that is the problem. Implemented one of the solutions suggested and made sure to empty the buffers. However the external program crashes in the same way. – liket May 29 '11 at 16:34
  • Does the process depend on environment variables? You do in that case need to provide those as well. How you do that is also explained in the article. Btw. Did you create two threads that are reading the treams concurrently? – Kaj May 29 '11 at 17:00
  • No it did not but after reading mKorbels suggestion and going over the article you suggested I found it was a problem with how I passed parameters to the program. Thank you very much for your help! – liket May 29 '11 at 19:24
1

this tutorial can help you with that http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1

and

ProcessBuilder

and

Oracle API

maybe my question Pass String as params from one Java App to another

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Thank you! It turned out it was an error in the way I passed parameters to the program. – liket May 29 '11 at 19:25