Run CMD / Java application using C#

Few days ago I needed to write a piece of code to call ANT file that builds some java files using the following CMD command ant –f build.xml. The process is rather straight forward task.  Use System.Diagnostics.Process that’s all, Easy.

To get the output result from the child process I redirect the standard output and the standard error and then read from the stream.

String output = process.StandardOutput.ReadToEnd();
String error = process.StandardError.ReadToEnd();

I developed my application under vista and after testing everything seems OK, until I moved it to the server (windows 2003). Whenever I run the process it hangs and never exist???

After struggling, the problem wasn’t OS related it was rather normal behavior if you try to read both stdoutput and stderr at the same time!! There problem is clearly explained at http://msdn2.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput(VS.80).aspx

The document suggests to create threads to handle this issue or alternatively use OutputDataReceived event, and so I did:

For complete source code listing please refer to the attachment

 Hope this helps

Attachment: ProcessInvoker.zip
Published Friday, February 08, 2008 12:40 PM by nawaf227
Filed under: , ,

Comments

# rascunho » Blog Archive » links for 2008-02-09

Saturday, February 09, 2008 3:39 PM by rascunho » Blog Archive » links for 2008-02-09

Pingback from  rascunho  » Blog Archive   » links for 2008-02-09

# re: Run CMD / Java application using C#

Friday, February 15, 2008 3:48 AM by komaar

Thank you for sharing this with us, this is what I was looking for.

# re: Run CMD / Java application using C#

Saturday, October 04, 2008 12:28 AM by kjarrodc

This has been very helpful for me.  Thank you very very much!

Leave a Comment

(required) 
(required) 
(optional)
(required)