0

I would like to make a GUI to host a Minecraft Server in Windows. Minecraft servers use a .jar file and a .bat file to run the .jar file and read output and give input to/from it.

How to make a C++ program that will open the .jar file, read the output and give input to it?

I tried with execlp, but when I #include <unistd.h> I get error that the "source file could not be read" (I think that this is because it is made for POSIX, but I'm not sure).

Any help would be appreciated!

(Also, just so you know, I'm very new to programming and C++)

davidweitzenfeld
  • 1,021
  • 2
  • 15
  • 38
  • JAR is a ZIP with a manifest file. Refer to http://stackoverflow.com/questions/262899/portable-zip-library-for-c-c-not-an-application for ZIP libraries. – Maxim Razin Mar 12 '13 at 21:00
  • @grep I don't want to zip files, I want to open the jar to work in the background, read the jar's output and give it some input. I don't see how that question relates to this. – davidweitzenfeld Mar 12 '13 at 21:04
  • When you said "open the jar to work in the background" you mean executing the jar as in `java -jar app.jar` but called from C++? – fmendez Mar 12 '13 at 21:07
  • @fmendez Yes. In a .bat file this would be `java -Xms1024M -Xmx1024M -jar craftbukkit.jar -o true`. – davidweitzenfeld Mar 12 '13 at 21:08
  • @davidwroxy Sorry about that, missed the point that was Windows. Perhaps this can help: http://stackoverflow.com/questions/7844884/run-a-executable-jar-from-c-code – fmendez Mar 12 '13 at 21:26
  • @fmendez Thank you, but unfortunately `JDK1_1InitArgs` doesn't exist in JDK 1.7. – davidweitzenfeld Mar 13 '13 at 15:27
  • As soon as I find a solution to this question, I will post it here as an answer. – davidweitzenfeld Mar 13 '13 at 18:50

1 Answers1

0

I managed to do this in C#. Here is the code (I think the C++ code would be quite similiar):

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();

For more info, please take a look here: https://servercrafter.codeplex.com/SourceControl/latest#ServerCrafter/ServerCrafter.ClassLibrary/ClassLibrary/Server/Server.cs

davidweitzenfeld
  • 1,021
  • 2
  • 15
  • 38