Liam Flaherty
Liam Flaherty

Reputation: 337

Process console output not redirecting

I am trying to execute a console/batch application from a BackgroundWorker in C# WPF application and show the output in a text box.

The batch application is as follows:

@echo off
echo start
"D:\openjdk-1.7.0-u40\bin\javac.exe"
echo finish

My C# code is:

Process process = new Process();
process.StartInfo.FileName = @"D:\exec.bat";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;

process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;

process.OutputDataReceived += new DataReceivedEventHandler(delegate(object sendingProcess, DataReceivedEventArgs outLine)
{
    backgroundWorker.ReportProgress(0, System.Environment.NewLine + outLine.Data);
});

process.Start();
process.BeginOutputReadLine();
process.WaitForExit();

I have also tried using this to read the console stream:

string output = process.StandardOutput.ReadToEnd();
backgroundWorker.ReportProgress(0, System.Environment.NewLine + output);

In my output, it displays start and finish, but no output from javac.exe, whereas if I run the batch file myself from cmd.exe it does.

How can I make the output of javac.exe appear?

Upvotes: 0

Views: 872

Answers (2)

Liam Flaherty
Liam Flaherty

Reputation: 337

After process.Start(); I added the following code to successfully produce an output:

do
{
    string output = process.StandardOutput.ReadToEnd();

    buildWorker.ReportProgress(0, System.Environment.NewLine + output);
}
while (!process.HasExited);

Upvotes: 2

user2513488
user2513488

Reputation:

Start javac.exe with parameter:

@echo off
echo start
start "D:\openjdk-1.7.0-u40\bin\javac.exe" /?
echo finish

Upvotes: 0

Related Questions