我认为这很简单,但我遇到了Process类的问题并重定向了stdout。我正在尝试运行java程序(Craftbukkit,Minecraft服务器软件)并重定向输出。
前两行的一切正常
210 recipes
27 achievements
但在该行后面的每一行都只是“>”
>
>
>
>
>
正确的输出应该如下所示
210 recipes
27 achievements
20:52:45 [INFO] Starting minecraft server version 1.4.6
20:52:45 [INFO] Loading properties
20:52:45 [INFO] Default game type: SURVIVAL
20:52:45 [INFO] Generating keypair
20:52:46 [INFO] Starting Minecraft server on *:25565
20:52:48 [INFO] This server is running CraftBukkit version git-Bukkit-1.4.6-R0.3
-5-g82c58b5-b2589jnks (MC: 1.4.6) (Implementing API version 1.4.6-R0.4-SNAPSHOT)
20:52:49 [INFO] Preparing level "world"
我输入一个断点来查看e.Data的值是什么,键入字符串,值“>”。所以问题不在于输出。
代码:
static void Main(string[] args)
{
ServerProcess p = new ServerProcess("craftbukkit.jar");
}
class ServerProcess
{
public ServerProcess(string server_jar)
{
ProcessStartInfo pInfo = new ProcessStartInfo("java", "-jar " + server_jar);
pInfo.RedirectStandardOutput = true;
pInfo.RedirectStandardError = true;
pInfo.UseShellExecute = false;
Process p = new Process();
p.StartInfo = pInfo;
p.OutputDataReceived += new DataReceivedEventHandler(ServerOutputDataReceived);
p.ErrorDataReceived += new DataReceivedEventHandler(ServerErrorDataReceived);
p.Start();
p.BeginOutputReadLine();
p.WaitForExit();
}
static void ServerErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine("Error: {0}", e.Data);
}
static void ServerOutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine("{0}", e.Data);
}
}
有人知道发生了什么吗?任何帮助将不胜感激。
答案 0 :(得分:1)
正常情况下,正常的非错误消息会像错误一样处理。 您添加了错误处理程序,但没有启动它。
p.BeginErrorReadLine();
编辑: 也可以尝试不带格式化的WriteLine
Console.WriteLine(e.Data);