Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to compile a java program using batch file. Code works if the program does not need any input but creates a deadlock condition when it needs input Here is the code:

C#
public void compilefile(ref System.Windows.Controls.RichTextBox rtb,ref TabItem tabitem)
   {
       var range = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);

       FileInfo filetocompile = new FileInfo(Path.GetTempPath() + tabitem.Header.ToString());
       using (FileStream fs = filetocompile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
       {
           range.Save(fs, System.Windows.DataFormats.Text);
           fs.Close();
       }

    try{

           pf = new ProcessStartInfo(@"E:\com.bat");
           pf.FileName = @"E:\com.bat";
           pf.Arguments = string.Format("{0} {1} {2} {3} {4}", "\"C:\\Program Files (x86)\\Java\\jdk1.7.0_13\\bin\\\"", "javac", tabitem.Header.ToString(), "java", tabitem.Header.ToString().Substring(0, tabitem.Header.ToString().LastIndexOf('.')));
           pf.CreateNoWindow = true;
           pf.ErrorDialog = false;
           pf.UseShellExecute = false;
           pf.RedirectStandardInput = true;
           pf.RedirectStandardOutput = true;
           pf.RedirectStandardError = true;
           pf.WorkingDirectory = Path.GetTempPath();
           p = new Process();
           p.StartInfo = pf;
           p.Start();
           input = p.StandardInput;
           string inputstring = "";
           input.AutoFlush = true;

           do
           {
               if (p.StandardError.ReadToEnd() != "")
               {
                   consoleTextBlock.IsDocumentEnabled = false;
                   consoleTextBlock.AppendText(p.StandardOutput.ReadToEnd());
                   consoleTextBlock.AppendText(p.StandardError.ReadToEnd());
                }
               else
               {
                   consoleTextBlock.AppendText(p.StandardOutput.ReadToEnd());
                   consoleTextBlock.AppendText(p.StandardError.ReadToEnd());

               }

               consoleTextInput.Focus();
               consoleTextBlock.ScrollToEnd();
               consoleTextBlock.IsReadOnly = true;
               System.Windows.MessageBox.Show(p.ExitCode.ToString());
           } while (p.HasExited != true);
       }
       catch (Exception ex)
       {
           System.Windows.MessageBox.Show(ex.ToString());

       }
   }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900