Pregunta

I thought this would be pretty straight-forward, but I'm having problems with the Process class and redirected stdout. I'm trying to run a java program (Craftbukkit, Minecraft server software) and redirect the output.

Everything works correctly for the first two lines

210 recipes
27 achievements

but after that line every line that follows is simply ">"

>
>
>
>
>

Correct output should look like this

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"

I put in a breakpoint to see what the value of e.Data was, type string, value ">". So the issue is not with outputting.

Code:

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);
    }

}

Does anybody know what is happening? Any help would be appreciated.

¿Fue útil?

Solución

It happens very often that normal, non-error messages are handled like errors. You added the error handler, but didn't started it.

p.BeginErrorReadLine();

EDIT: Also try WriteLine without formatting

Console.WriteLine(e.Data);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top