我设法在 C# 中做到了这一点。这是代码(我认为 C++ 代码会非常相似):
var arguments = "-jar -Xms" + Settings.ServerStartInfo.InitialRam + "M -Xmx" +
Settings.ServerStartInfo.MaximumRam + "M \"" + Settings.ServerStartInfo.FileName +
"\" -nojline" + Settings.ServerStartInfo.Arguments;
var processStartInfo = new ProcessStartInfo
{
FileName = "javaw.exe",
Arguments = arguments,
CreateNoWindow = true,
ErrorDialog = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
StandardOutputEncoding = Encoding.UTF8,
StandardErrorEncoding = Encoding.UTF8,
UseShellExecute = false,
WorkingDirectory = Settings.ServerStartInfo.WorkingDirectory
};
Process = new Process { StartInfo = processStartInfo };
Process.OutputDataReceived += ServerOutputHandler.ServerOutputReceived;
Process.ErrorDataReceived += ServerOutputHandler.ServerOutputReceived;
Process.Start();
Process.BeginOutputReadLine();
Process.BeginErrorReadLine();
Process.WaitForExit();
Process.Start();
有关更多信息,请查看此处:https ://servercrafter.codeplex.com/SourceControl/latest#ServerCrafter/ServerCrafter.ClassLibrary/ClassLibrary/Server/Server.cs