Hello, I'm using a process to compile java source code from within my program. I am then attempting to read the output ( errors , if it compiled ok etc) . But for some reason it isn't working. It works fine when using other shell commands ( ping etc ) but no output is displayed with javac.

Process process = new Process();

                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.FileName = "javac.exe";
                process.StartInfo.Arguments = "source.java";

                process.Start();

                MessageBox.Show(process.StandardOutput.ReadToEnd()); // for testing

Any suggestions?


: Edit problem solved was using standardoutput instead of error =/

Dani AI

Generated

Short summary and practical follow-ups based on the thread: found the root cause — javac prints compilation diagnostics to the process StandardError stream (and prints nothing on success), so reading only StandardOutput will appear empty. For a reliable solution, redirect and read both streams and handle them correctly so output isn’t lost or the app deadlocks.

Key points and patterns to follow:

  • Ensure UseShellExecute = false and set both RedirectStandardOutput and RedirectStandardError when capturing output.
  • Prefer asynchronous reads when both streams are redirected to avoid blocking (the MSDN ReadToEnd + WaitForExit pattern works for small output but can deadlock on large output). Attach OutputDataReceived and ErrorDataReceived handlers and start asynchronous reads after the process starts, for example:
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
  • With event-based reading check for e.Data != null (end-of-stream is signaled with null) and marshal UI updates back to the main thread (RichTextBox updates must use Invoke/BeginInvoke). This addresses the situation in ’s post where handlers were added but the Begin*ReadLine calls were not shown.
  • Capture Process.ExitCode to determine success (compilers typically return 0 for success, nonzero for errors) and verify javac is reachable (full path or JDK bin on PATH), arguments are quoted if they contain spaces, and WorkingDirectory and file existence are correct.

Troubleshooting checklist:

  • Confirm which stream the tool writes to (stderr vs stdout).
  • Try a known small program that writes to stderr to verify redirection.
  • If using ReadToEnd, read both stdout and stderr before WaitForExit to avoid deadlock.

These steps explain why the original sample showed no output and tie back to the approaches suggested by and while avoiding the common pitfalls seen in the thread.

Recommended Answers

All 5 Replies

Hi,

I have a similar problem. I try to run a Program and I want to see its output in a RichTextBox.

void start_process(){
    ProcessStartInfo psi = new ProcessStartInfo();
    psi.FileName = "path/to/*.exe";
    psi.WorkingDirectory = "path/to/working_directory";
    psi.UseShellExecute = false;                    
    psi.RedirectStandardOutput = true;         

    Process myProc = Process.Start(psi);      

    myProc.OutputDataReceived += new DataReceivedEventHandler(myProc_OutputDataReceived);
        // this is a suggestion of MS Visual C# Studio 2005 ^^
}

void myProc_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    RichTextBox1.AppendText(e.Data+"\n");
}

Thanks!

opatut

Hi,

I have a similar problem. I try to run a Program and I want to see its output in a RichTextBox.

void start_process(){
    ProcessStartInfo psi = new ProcessStartInfo();
    psi.FileName = "path/to/*.exe";
    psi.WorkingDirectory = "path/to/working_directory";
    psi.UseShellExecute = false;                    
    psi.RedirectStandardOutput = true;         

    Process myProc = Process.Start(psi);      

    myProc.OutputDataReceived += new DataReceivedEventHandler(myProc_OutputDataReceived);
        // this is a suggestion of MS Visual C# Studio 2005 ^^
}

void myProc_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    RichTextBox1.AppendText(e.Data+"\n");
}

Thanks!

opatut

// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();


From MSDN Library.

// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();


From MSDN Library.

// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();


From MSDN Library.

Thanks royael. Problems is solved. Its succesfull...

commented: Don't bump old threads -1

I'm glad you got it helpful. Please do not resurrect old threads. If you have any questions please ask. .... You are welcome to start your own threads.

Please read the rules before posting again - http://www.daniweb.com/forums/thread78223.html and rules.

Thread Closed.

commented: you seem to have a patience of a saint to keep closing dozen of "Grand returns" and remain same unflappable :D +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.