进程未启动

Process doesn't start

我们正在尝试启动一个进程,但它就是没有启动。当然还有更多方法可以更改 currentIndex 等,但是如果我们添加它们,就会有很多代码...

public class Server
{
    public bool isRunning { get; set; }
    private Process p;
    public string Name { get; set; }
    public string Path { get; set; }

    public string Output { get; private set; }

    public event DataReceivedEventHandler OutputDataReceived;
    public event DataReceivedEventHandler ErrorDataReceived;

    protected virtual void OnReceived(object sender, DataReceivedEventArgs e)
    {
        if (OutputDataReceived != null)
        {
            OutputDataReceived(Name, e);
            Output += e.Data + "\n";
        }
    }
    protected virtual void OnError(object sender, DataReceivedEventArgs e)
    {
        if (ErrorDataReceived != null)
        {
            ErrorDataReceived(Name, e);
            Output += e.Data + "\n";
        }
    }

    public Server(string name, string path)
    {
        p = new Process();
        this.Name = name;
        this.Path = path;

        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.RedirectStandardInput = true;

        p.StartInfo.UseShellExecute = false;
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.FileName = @"C:\Program Files (x86)\Java\jre1.8.0_25\bin\java.exe";

        p.OutputDataReceived += OnReceived;
        p.ErrorDataReceived += OnError;
    }

    public bool Start()
    {
        try
        {
            if (isRunning == false)
            {
                p.StartInfo.WorkingDirectory = Path;
                p.StartInfo.Arguments = @"-Xmx1024M -jar " + System.IO.Path.GetFileName(getJarFile()) + " -nojline";

                bool b = p.Start();
                if (b == false) throw new ApplicationException();
                p.BeginOutputReadLine();

                isRunning = true;
                return true;
            }
            else { return false; }
        }

        catch (Exception ex) { System.Windows.Forms.MessageBox.Show("An error occured while starting the server: " + ex, "Error"); return false; }
    }

    public bool Stop()
    {
        try
        {
            if (isRunning == true)
            {
                using (System.IO.StreamWriter sw = p.StandardInput)
                {
                    sw.WriteLine("/stop");
                    isRunning = false;
                }

                return true;
            }

            else { return false; }
        }

        catch (Exception ex) { System.Windows.Forms.MessageBox.Show("An error occured while stopping the server: " + ex, "Error"); return false; }
    }

    private string getJarFile()
    {
        string path = "ERROR";

        Console.WriteLine(Path);

        foreach (string fp in System.IO.Directory.GetFiles(Path))
            if (fp.EndsWith(".jar"))
                path = fp;

        Console.WriteLine(path);
        return path == "ERROR" ? null : path;
    }
}

表格class:

public partial class MainForm : Form
{
    public List<Server> servers = new List<Server>();
    public int currentIndex = -1;

    delegate void AddConsoleTextCallback(string text);

    private void Form1_Load(object sender, EventArgs e)
    {
        foreach (string path in Directory.GetDirectories(Program.programPath + "\Servers"))
            servers.Add(new Server(Path.GetFileName(path), path));
        foreach (Server s in servers)
            serversComboBox.Items.Add(s.Name + " (Idle)");
        foreach (Server s in servers)
        {
            s.OutputDataReceived += s_DataReceived;
            s.ErrorDataReceived += s_DataReceived;
        }
    }

    private void AddText(string text)
    {
        if (this.consoleTextBox.InvokeRequired)
        {
            AddConsoleTextCallback d = new AddConsoleTextCallback(AddText);
            this.Invoke(d, text);
        }
        else
        {
            this.consoleTextBox.Text += text;
        }
    }

    void s_DataReceived(object sender, DataReceivedEventArgs e)
    {
        if ((string)sender == servers[currentIndex].Name)
            this.AddText(e.Data + "\n");
    }

    private void button1_Click(object sender, EventArgs e) // Start
    {
        if (currentIndex == -1)
        {
            MessageBox.Show("No server is selected!", "Error");
            return;
        }

        try 
        { 
            if (servers[currentIndex].Start())
            {
                startButton.Enabled = false;
                if (((string)serversComboBox.Items[currentIndex]).Contains(" (Idle)"))
                    serversComboBox.Items[currentIndex] = 
                        ((string)serversComboBox.Items[currentIndex]).Replace(" (Idle)", " (Running)");
                else if (((string)serversComboBox.Items[currentIndex]).Contains(" (Running)"))
                    serversComboBox.Items[currentIndex] =
                        ((string)serversComboBox.Items[currentIndex]).Replace(" (Running)", " (Idle)");
            }
        }
        catch (Exception ex) { MessageBox.Show("An error occured: " + ex); }
    }
}

P.S。这可能是一个愚蠢的错误,MainForm class 中的委托是直接从互联网上获取的,所以我们不知道是否有必要这样做。但是,仍然会非常感谢您的帮助:D

编辑:

我们已经解决了部分问题:p.StartInfo.WorkingDirectory。但是我们现在有另一个错误:.jar 只是显示(如在表单中,这不应该发生),并且它在 .jar 本身中生成了很多错误(太多无法列出)。

我猜失败的文件位于带空格的路径中。将 path = fp; 替换为 path = "\"" + fp + "\"";