Ken

Ken

  • NA
  • 110
  • 0

Java app console redirect in C# application?

Apr 8 2011 4:47 PM
Hi.  I am trying to open a command window and read/write it from a C# forms app.  I have a textbox "textBox1" to display output and "textBox2" for me to type commands to the cmd process.  This is the code:

ProcessStartInfo info = new ProcessStartInfo("cmd");
info.UseShellExecute = false;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.CreateNoWindow = true;

proc = Process.Start(info);
sw = proc.StandardInput;
string buf = "";
StreamReader sr = proc.StandardOutput;

while (serverrunning) {
  buf = sr.ReadLine();
  textBox1.Text += buf + Environment.NewLine;
  textBox1.SelectionStart = textBox1.Text.Length;
  textBox1.ScrollToCaret();
  Application.DoEvents();
}

This works for regular DOS stuff.  When I run it, I get the "Microsoft Windows [Version 6.1.7600], Copyright" etc etc command prompt header.  Only issue is I am not receiving the last line of text until I send a CR, I assume because ReadLine is waiting for that to flush its buffer.  I can live with that, but then I launch my java app and that is where the real problem starts. 

The java app is a Minecraft server.  When you fire it up (java -Xms1G -Xmx1G craftbukkit.jar) regularly I get this:


16:42:46 [INFO] Starting minecraft server version Beta 1.4
16:42:46 [INFO] Loading properties
16:42:46 [INFO] Starting Minecraft server on *:25565
16:42:46 [INFO] This server is running Craftbukkit version git-Bukkit-0.0.0-646-gb61ef8c-b670jnks (M
C: 1.4)
16:42:46 [INFO] Preparing level "world"
16:42:46 [INFO] Preparing start region
16:42:46 [INFO] 144 recipes
16:42:47 [INFO] Preparing spawn area: 89%
16:42:47 [INFO] Done (0.117s)! For help, type "help" or "?[/code]

Problem is, when I run it from my C# linked command process I am seeing this in my textbox:

Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

c:\dropbox\projects\MinecraftManager\bin\Debug>cd\applications\minecraft\server

c:\Applications\Minecraft\Server>java -Xms1G -Xmx1G craftbukkit-0.0.1-SNAPSHOT.jar
>
>
>
>
>
>
>
>
>

I guess java doesn't do stdout right or something.  Is there a way around this?


Answers (4)