I had to send a command to Spigot's console for interprocess communication, so I'll leave that method.
Server.java
public class Server {
private Process process = null;
private BufferedWriter commandLine;
public void boot() {
ProcessBuilder pb = new ProcessBuilder("java","-jar","~.jar");
pb.directory(folder);
try {
process = pb.start();
} catch (IOException e) {
e.printStackTrace();
}
commandLine = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
}
public void sendCommand(String command) {
try {
commandLine.write(command + "\n");
commandLine.flush();
} catch (IOException e){
e.printStackTrace();
}
}
public void stop() {
if (process == null) return;
sendCommand("stop");
try {
commandLine.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
A fairly suitable code
Commands can be sent by writing to the process's OutputStream using BufferedWriter. This is starting the server externally, but it should be possible for processes that are already running ~~ Note that it will not be executed unless a line break occurs
Recommended Posts