2

I am working on an application that starts a minecraft server with one click of a button. I've successfully got the server to startup, but now I am trying to figure out a way to stop the server through the same cmd process.

Here's my code for starting the server...

public static void startServer() {
    System.out.println("Starting server...");
    try {
        server = Runtime.getRuntime().exec(
                "java -jar -Xmx1024M -Xms1024M minecraft_server.jar nogui");
        output = server.getOutputStream();
        input = server.getInputStream();
        writer = new BufferedWriter(new OutputStreamWriter(output));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

This tells the runtime to execute a run.bat file that is in the same directory of the application. This method also initialized the OutputStream and InputStream objects that I created at the top of this class.

Here's my code for stopping the server...

public static void stopServer() {
    System.out.println("Stopping server...");
    // server.destroy();
    try {
        writer.write("stop\n\r");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

"stop" is a command that I'm trying to issue to the server to stop it, but for some reason the command is never being issued to the server.

More info:

  • The server is being run in cmd.exe, and therefore all server cmds need to be issued in cmd.

  • The server is named minecraft_server.jar so I have to use the command line to run the server and get output from the server and write input to it.

  • The run.bat file contains the text java -Xmx1024M -Xms1024M -jar minecraft_server.jar nogui.

  • My main goal is to write the command "stop" to the server to stop it.

TTG_TRE
  • 19
  • 5
  • 1
    it might be that since you are calling `server.destroy()`, the process never waits for the "stop" command to finish executing. You might want to try removing the destroy call, or possibly after writing stop, try waiting for the process to finish via `server.waitFor()`. – clearlyspam23 Jul 18 '14 at 16:37
  • This should help: http://stackoverflow.com/questions/9305413/java-exec-use-input-redirect – Moe Singh Jul 18 '14 at 16:37
  • I am not sure how to do this in windows but in bash I would find the pid of the running instance and kill that pid so the command to translate might be `kill' or `killall`if the path to your program name is set up already I guess. – Magpie Jul 18 '14 at 16:38
  • Debug one step at a time. Tryreplacing run.bat's content with a batch file loop that "simulates" a server like `:tryagain
    pause
    echo hello
    goto tryagain`. Then see if sending CR/LF causes "hello" to appear on the input stream. Whether this works or doesn't, you've ruled out about half the possible problem causes in each case.
    – Gene Jul 18 '14 at 17:19

1 Answers1

1

As @clearlyspam23 stated, you are killing the process the moment you write anything to it.

Second, you are writing to the process' output stream, you want to write in the input stream. Edit: nope

Also, any server command is usually validated with a 'Enter' keystroke, so you might need to add a carriage return ('\r') right after your command to simulate that.

Snaipe
  • 1,191
  • 13
  • 25
  • 1
    actually, as messed up as it sounds, [process.getOutputStream](http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html#getOutputStream()) returns the subprocess's standard input, so I believe he actually is writing to the correct stream – clearlyspam23 Jul 18 '14 at 16:45
  • I updated my code for the stopServer method and tried running my code and still nothing is happening. Any other suggestions? Am I doing something wrong? Thank you for your efforts. By the way, there is an application window with a start button and a stop button. Start button calls the `startServer()` method and stop button calls the `stopServer()` method. – TTG_TRE Jul 18 '14 at 16:56
  • @user2259637 I realized you were under windows, so try \n and \n\r instead of \r. Also, is the .bat file properly executed ? maybe add an echo inside the .bat file, and print the process' stdout. – Snaipe Jul 18 '14 at 17:05
  • Thank you for you effort, but I'm still having no luck. I'm going to send you a link to my application. Put the minecraft_server.jar and ServerHelper in the same folder. http://www.mediafire.com/download/ya1yk1xikabyseb/ServerHelper.zip – TTG_TRE Jul 18 '14 at 17:16
  • @user2259637 Hah, but I'm running linux, so I won't be any help. Are you sure that the .bat file is actually run ? – Snaipe Jul 18 '14 at 17:23
  • Yes. Wen I run the batch file, it starts the server up. I just don't know how to execute a command to the batch file. Thank you. – TTG_TRE Jul 18 '14 at 17:31
  • @user2259637 have you tried to call exec("java -jar minecraft_server.jar nogui") and use its streams instead of calling the .bat ? – Snaipe Jul 18 '14 at 17:40
  • Ok, I updated my code with what is shown above. I'm able to call `server.destroy` which I wasn't able to do before, and I am not starting the run.bat anymore. Instead I'm starting the minecraft_server.jar. But I am still unable to write to it's output stream. I chose to create a `BufferedWriter` and an `OutputStreamWriter` to see if that would do anything, but it hasn't. Thank you for your effort. – TTG_TRE Jul 18 '14 at 19:49
  • I think I found my issue. I think it's the fact that I'm using the Java 8 when the server is compatible with 1.6, and therefore making it so I can't write commands to the server console within my application. – TTG_TRE Jul 18 '14 at 22:40