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.
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