Brilliance Of Ben
Brilliance Of Ben

Reputation: 91

Console Commands Minecraft Server Start/Stop Through Java GUI

I can't for the life of me figure out how to start a nd stop a server for the game Minecraft using two buttons in Java.

So far I have this mess..

try
        {
            ProcessBuilder processBuilder = new ProcessBuilder("/Users/UserName/Desktop/servers/test/launch.sh");
            Process server;
            if (event.getSource() == start_Btn)
            {
                server = processBuilder.start();
                //OutputStream out = server.getOutputStream();
                start_Btn.setText("Started");

            }   
            else if (event.getSource() == stop_Btn)
            {

                OutputStream out = server.getOutputStream();
                server.getOutputStream().write(new String("stop").getBytes("utf-8"));

                stop_Btn.setText("Stoped");
                start_Btn.setText("Start");

            }
        }
        catch (IOException exception)
        {

        }
        catch (InterruptedException exception)
        {

        }

I have been scouring the internet for the entire day today and I've decided to finally bring it to you guys.

I want to be able to start the server by pressing a "Start" button, then stop it with a "Stop" button I have a GUI set up and I know how to set up button events. I can get the server to start with the start button easily, it is just the stopping feature I can't seem to manage.

Note: To stop the server you must enter in "stop" in the command line where the server was initiated.

Thank you very much for your help, I greatly appreciate it.

Upvotes: 0

Views: 4824

Answers (2)

Brilliance Of Ben
Brilliance Of Ben

Reputation: 91

Seeing as though there never was an answer that solved my question, and seeing as though I figured it out on my own I figured I'd post it for everyone else happening on the question.

I use a couple classes to accomplish this goal, two to be exact.. One to be the thread that houses the server and the other to send commands to the server.

First things first, the code to start and house the server stream.

The first class here is where the magic happens

public class Sender{

ConsoleWriter cWriter = new ConsoleWriter();
public void execute(){
    this.ui = ui;
    try{
        ProcessBuilder pb = new ProcessBuilder(path_to_server+"launch.bat");
        Process process = pb.start();

        StreamGobbler sgError = new StreamGobbler(process.getErrorStream());

        new Thread( sgError ).start();
        writer = new PrintWriter( process.getOutputStream() );

    } catch ( IOException e ){
        e.printStackTrace();
    }
}

private class StreamGobbler implements Runnable 
{
    private InputStream is;
    public StreamGobbler( InputStream is ){
        this.is = is;
    }
    @Override
    public void run() {
        try {
            InputStreamReader isr = new InputStreamReader( is );
            BufferedReader br = new BufferedReader( isr );
            String line = null;
            while ( ( line = br.readLine() ) != null ){
                cWriter.writer(line, ui);
            }
        } catch ( IOException e ){
            e.printStackTrace();
        }
    }
}

}

How does this work you ask? Let's take it from the top.

  • ConsoleWriter

This is the class I wrote to write the stream from Minecraft's server console to my own GUI, the code for this isn't important so I'll leave it out. You can write that part.

  • execute()

This method builds the process for the server using Java's ProcessBuilder then starting the process. More importantly, we have the StreamGobbler.. This gives us access to and 'gobbles up' the input stream from the server. Basically it receives all the output the console from the server. For some reason, not sure why, the Minecraft server likes the ErrorStream so I've bound it to that. Then I create a new Thread for the gobbler and that's that. Last thing in this method is the...

  • PrinterWriter

This binds to the Server as an output which let's me send commands to the server like for stopping it or really any other server command available.

  • StreamGobbler class

Now, onto the Gobbler its self. Not too much here. Basically just taking the inputStream we sent from the execute method sending it to a reader, then buffering it and finally reading it to my console writer.

The second Class is quite simple!

public class WriteCommand
{
public void send(String command)
{
    txtA.append("Command:>>"+ command + "\n");
    writer.write(command);
    writer.write("\n");
    writer.flush(); 
}
}

All this is doing is writing the command and hitting 'enter' then flushing it to be ready to send the next! txtA.append is for adding the command that was sent to my own console output simply a visual item.

And there you go! Hopefully this will help someone else out. If you'd like to see this code in action you can see it as part of the app I've used it in.

Here is a link: Minecraft Server Utility(BETA)1.3.6

Upvotes: 2

Frank
Frank

Reputation: 59

I was working on this same task today. I am a novice at java but I think I found what you may be missing. I more or less followed your lead but in the stop command use the slash "/stop"

also it seems that I needed to close the outputstream in order for the action to complete.

private void stopButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
    // TODO add your handling code here:
    try {
        oS.write(new String("/stop").getBytes("utf-8"));
        oS.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}               

I hope that this helps you.

Upvotes: 0

Related Questions