Running a minecraft server from within my own program
Asked Answered
G

1

6

I am trying to set up a minecraft server, for fun, and want to add a control panel to make managing it easier. Part of this control panel is to run the server FROM the panel to reduce miss-clicking the exit button and what-not. (the minecraft server is a non-runnable .jar file, implying you have to make a .bat to run it from the command line) I know how to run the server inside my program, as well as print the output, here:

ProcessBuilder pb = new ProcessBuilder("java", "-jar", "gscale.jar");
        pb.redirectErrorStream(true);
        pb.directory(new File("F:\\Documents and Settings\\Administrator\\Desktop"));

        System.out.println("Directory: " + pb.directory().getAbsolutePath());
        Process p = pb.start();
        InputStream is = p.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        for (String line = br.readLine(); line != null; line = br.readLine()) {
                System.out.println( line ); // Or just ignore it
        }
        p.waitFor();

but, with this, i'm not sure how to implement an input method. for example, if I click a button, it sends the input "stop" to the external jar to stop the server, as it does without this command panel. how would I implement that? the code above I plan to be running in it's own thread, but maybe i'm doing this all wrong?

Thanks in advance!

Gorham answered 15/1, 2014 at 16:43 Comment(1)
Do you have your code on an open source platform such as github? Would you be willing to? I'm trying to do something similar. So that I can run a single server with a number of mod packs that would all run just one at a time on demand. If you'd be willing to share your code I'd love to add to it and of course I would be willing to share my collaborations.Semipro
H
5

Looks like the Process class has a getOutputStream() method. You could try something like this:

OutputStream os = p.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
bw.write("stop");
bw.newLine();
bw.flush();
Hatten answered 15/1, 2014 at 17:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.