Click here to Skip to main content
15,896,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im trying to get output from my minecraft server console to by console gui
any suggestions?

C#
private void button_start_Click(object sender, EventArgs e)
        {
            this.button_start.Enabled = false;

            using (Process sortProcess = new Process())
            {
                sortProcess.StartInfo.FileName = @"C:\Users\Nico Travassos\Desktop\Minecraft\Minecraft CROSS Server\RUN.bat";
                sortProcess.StartInfo.CreateNoWindow = true;
                sortProcess.StartInfo.UseShellExecute = false;
                sortProcess.StartInfo.RedirectStandardOutput = true;

                // Set event handler
                sortProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);

                // Start the process.
                sortProcess.Start();

                // Start the asynchronous read
                sortProcess.BeginOutputReadLine();

                sortProcess.WaitForExit();
            }
        }
        void SortOutputHandler(object sender, DataReceivedEventArgs e)
        {
            Trace.WriteLine(e.Data);
            this.BeginInvoke(new MethodInvoker(() =>
            {
                richTextBox1.AppendText(e.Data ?? string.Empty);
            }));
        }


Trying to run my minecraft server in my custom gui i made but every time i click start its starts the RUN.bat and gives a error "Error: unable access jarfile craftbukkit.jar"

this is my run file code.

VB
@ECHO OFF
"C:\Program Files (x86)\Java\jre7\bin\java.exe" -Xmx1024M -Xms512M -Xmn256M -XX:+UseParNewGC -XX:+DisableExplicitGC -XX:+UseFastAccessorMethods -XX:+UseConcMarkSweepGC -XX:UseSSE=3 -XX:ParallelGCThreads=2 -jar craftbukkit-1.6.4-R1.0.jar nogui
PAUSE
Posted
Updated 29-Oct-13 10:14am
v3

where is
craftbukkit-1.6.4-R1.0.jar
in relation to your exe ? no, thats badly phrased - how will the jre know where to find that jar - is it on your CLASSPATH, or, have you specified a CLASSPATH in your java.exe run statement for-instance ?

The commonest mistake I see where people are starting 'processes' is they assume the environment is as they see it when they are logged on - and unless you're actually forcing the process to be run under a different profile, and that profile has everything set up correctly, thats usually not the case

'g'
 
Share this answer
 
Comments
dev.nic 29-Oct-13 17:00pm    
craftbukkit-1.6.4-R1.0.jar is inside folder if you click run.bat it runs craftbukkit-1.6.4-R1.0.jar creating the server it works fine if i click it but if i click start in my gui it does not work the same
Garth J Lancaster 29-Oct-13 17:07pm    
which folder ? if you mean "C:\Users\Nico Travassos\Desktop\Minecraft\Minecraft CROSS Server\" I still dont see anything there that either sets the CLASSPATH or sets 'the execution context' to that folder ... you're making too many assumptions about the process you're starting

Does it work if you add

c:
CD "\Users\Nico Travassos\Desktop\Minecraft\Minecraft CROSS Server"

as the very first 2 lines to run.bat ?OR ... see if using/adding


sortProcess.StartInfo.WorkingDirectory="C:\Users\Nico Travassos\Desktop\Minecraft\Minecraft CROSS Server\";

to the process params/startup helps
dev.nic 29-Oct-13 17:18pm    
It works when I click the Run.bat the server runs but why is it with my code start button when I click it it goes to file but shows whats in the run.bat file and not run server
Maby this helps you ^^
Make a textbox and name it: "serverlog" for output data from server
Make a textbox and name it: "msgtb" to write commands to server
Make a button and name it: "Startbt" to Start the server
Make a button and name it: "Stopbt" to stop the server



C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.IO;
using System.Diagnostics;


namespace MinecraftServer
{
    public partial class MinecraftServer : Form
    {
       
        StreamWriter WriteCommands;
        BackgroundWorker StartServer = new BackgroundWorker();

        delegate void SetTextCallBack(string text);

        string MaxRam = "1024";
        string MinRam = "1024";
        string server = @"C:\Users\LetsPlayLittle\Desktop\server\server.jar";

        public MinecraftServer()
        {
            InitializeComponent();
            StartServer.DoWork += StartServer_DoWork;
        }

        void StartServer_DoWork(object sender, DoWorkEventArgs e)
        {
            for (var i = 0; i < 1; i++)
            {
                Process Minecraft = new Process();
                Minecraft.StartInfo.FileName = "CMD.exe";
                Minecraft.StartInfo.CreateNoWindow = false;
                Minecraft.StartInfo.RedirectStandardInput = true;
                Minecraft.StartInfo.RedirectStandardOutput = true;
                Minecraft.StartInfo.RedirectStandardError = true;
                Minecraft.StartInfo.UseShellExecute = false;
                Minecraft.OutputDataReceived += Minecraft_OutputDataReceived;
                Minecraft.ErrorDataReceived += Minecraft_ErrorDataReceived;
                Minecraft.Start();
                Minecraft.BeginOutputReadLine();
                Minecraft.BeginErrorReadLine();
                WriteCommands = Minecraft.StandardInput;
                WriteCommands.WriteLineAsync(@"java -Xmx" + MaxRam + "M -Xms" + MinRam + "M -jar " + server + " " + "nogui");
               
            }
        }

        void Minecraft_ErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
           
        }

        void Minecraft_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            try
            {
                this.SetText(msgtb.Text + e.Data + System.Environment.NewLine);
            }
            catch (Exception ex)
            {
            }
        }
        private void SetText(string text)
        {
            if (this.serverlog.InvokeRequired)
            {
                SetTextCallBack d = new SetTextCallBack(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                serverlog.Text += text + System.Environment.NewLine; 
            }

        }
        
        private void Startbt_Click(object sender, EventArgs e)
        {
            StartServer.RunWorkerAsync();
        }

        private void Stopbt_Click(object sender, EventArgs e)
        {
            WriteCommands.WriteLineAsync("/stop");
        }
        
        private void msgtb_Enter(object sender, EventArgs e)
        {
            WriteCommands.WriteLineAsync(msgtb.Text);
        }
    }


}
 
Share this answer
 
v2
Comments
Member 14568719 27-Dec-19 8:57am    
Hey, im used your code above a while ago and it worked fine, now it said that this: for (var i = 0; i & lt; 1; i++) on line 37 is wrong (the lt the 1 and the i++) can u help me ?

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