Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a minecraft server from within my own program

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!

like image 447
PulsePanda Avatar asked Jan 15 '14 16:01

PulsePanda


People also ask

Can you run a Minecraft server on your own computer?

A Minecraft server is a game server made to specifically host online multiplayer lobbies of Minecraft. By using some software from Mojang Studios, the video game developer of Minecraft, players can choose to use their own computers as Minecraft servers.

Can you locally host a Minecraft server?

Click the Multiplayer tab, and then the Add Server option. Key in the hosting computer's IP address or simply type "localhost" in the server address bar.

Should I self host a Minecraft server?

Self-hosting a Minecraft server is the cheapest method. It's a free option that allows you run a server on your personal computer and WiFi network. However, there are some cons to self-hosting a Minecraft server. If you want to keep your server online, you will also need to keep the device on.


1 Answers

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();
like image 159
Mike B Avatar answered Oct 23 '22 11:10

Mike B