我认为这很简单,但是我在 Process 类和重定向标准输出方面遇到了问题。我正在尝试运行一个 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);
}
}
有人知道发生了什么吗?任何帮助,将不胜感激。