Reputation: 475
We are trying to start a process, but it just doesn't start. There are ofcourse more methods for changing the currentIndex etc., but if we added them there would be way to much code...
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;
}
}
Form 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. It is probably a stupid mistake, and the delegate in the MainForm class in taken directly from the internet, so we have no idea if it is even necessary. But still, help would be really appreciated :D
Edit:
We have figured out part of the problem: p.StartInfo.WorkingDirectory. But we have another error now: The .jar just shows (as in the form, which shouldn't happen), and it generates ALOT of errors in the .jar itself (there are just too many to list).
Upvotes: 0
Views: 86
Reputation: 579
I'm guessing the failing files are in a path with spaces. Replace path = fp;
with path = "\"" + fp + "\"";
Upvotes: 2