如何使用C ++打开,读取输出并将输入提供给.jar文件?

时间:2013-03-12 20:54:53

标签: c++ jar c++-cli minecraft

我想制作一个GUI来在Windows中托管Minecraft服务器。 Minecraft服务器使用.jar文件和.bat文件来运行.jar文件并读取输出并向其提供输入。

如何创建一个打开.jar文件的C ++程序,读取输出并给它输入?

我尝试使用execlp,但是当我#include <unistd.h>时,我收到“源文件无法读取”的错误(我认为这是因为它是为POSIX制作的,但我是不确定)。

任何帮助将不胜感激!

(另外,你知道,我是编程和C ++的新手)

1 个答案:

答案 0 :(得分:0)

我设法用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