Wysyłanie poleceń do aplikacji konsoli?
Chcę coś zweryfikować, bo w mojej głowie ma to sens, ale w Javie to nie działa.
Próbuję uruchomić inny plik Jar za pośrednictwem mojej aplikacji. Dokładnie serwer Minecraft. Mam wszystkie podstawy w dół (za pomocąProcessBuilder
, wykonywanie z argumentami, oczekiwanie na kod wyjścia itp.), ale jest jedna rzecz, której nie mogę zrozumieć. Wysyłanie poleceń do aplikacji. Oto część mojegoCommandLineSender
klasa:
public class CommandLineSender extends Thread {
private BufferedWriter output;
private InputStream source; // Set to System.in when creating the object
private boolean stopRequested;
public CommandLineSender(Process sendTo, InputStream source) {
this.output = new BufferedWriter(new OutputStreamWriter(sendTo.getOutputStream()));
this.source = source;
System.out.println("Source InputStream initiated: " + source.toString());
this.stopRequested = false;
}
@Override
public void run() {
System.out.println("Run called.");
Scanner cmdScanner = new Scanner(source);
while (cmdScanner.hasNextLine() && !stopRequested) {
System.out.println("Has next line");
String msg = cmdScanner.nextLine();
write(msg);
System.out.println("Wrote: " + msg);
}
// Close the scanner and BufferedWriter
System.out.println("Closed.");
}
// Other various methods
protected void write(String msg) {
try {
output.write(msg);
} catch (IOException e) {
System.err.println("Unable to write message because of an unhandled IOException: " + e.getMessage());
}
}
Otrzymuję wynik:
(Default Minecraft server output)
help // My command
Has next line
Wrote: help
To może nie mieć znaczenia, ale wykonuję mój serwer z tymi argumentami:
java -Xmx1024M -Xms1024M -jar (path to server jar) nogui
Dziękuję za Twój czas.