我已经搜索了很多答案,但没有发现任何真正有用的东西,我发现的线程都是僵尸。
我的问题:我正在为Bukkit服务器创建一个UI,但是对于最终用户在服务器中执行命令,我必须使用OutputStreamWriter写入该进程。 我有所有代码,但命令没有被发送到进程。
到目前为止,这是我用来执行服务器并获取所有流的代码:
@SuppressWarnings("null")
private void startServer() throws InterruptedException, IOException {
setTitleText("BukkitUI by Beatsleigher [Starting...]");
setStatusImg(ServerState.BOOTING);
Thread.sleep(1000);
setTitleText("BukkitUI by Beatsleigher [Loading components...]");
runServer = true;
process = new ProcessBuilder();
// Set attibutes
process.redirectErrorStream(true);
java.util.List<String> jvmArgs = new ArrayList<>(4);
jvmArgs.add(JVMManager.getJava().toString()); // If you don't know what this does, you should not consider yourself a programmer.
jvmArgs.add("-jar");
jvmArgs.add(JVMManager.getProcessedInitHeapSize()); // Add Java VM arg[0]
//jvmArgs.add(JVMManager.getProcessedMaxHeapSize()); // Add Java VM arg[1]
jvmArgs.add(PreferenceManager.getBukkitLocation().getAbsolutePath()); // Add Java VM arg[2]
jvmArgs.add("-o"); // Add CraftBukkit arg[0]
jvmArgs.add("true"); // Add CraftBukkit arg[1]
process.command(jvmArgs);
// Start process
pr = process.start();
setTitleText("BukkitUI by Beatsleigher [Running]");
consoleWriter = new BufferedWriter(new OutputStreamWriter(pr.getOutputStream()));
processServer();
}
private void processServer() throws IOException {
new Thread() {
@Override
public void run() {
try {
setStatusImg(ServerState.ONLINE);
jTextPane1.setText("");
consoleReader = new BufferedReader(new InputStreamReader(pr.getInputStream()));
while ((consoleOutput = consoleReader.readLine()) != null && (runServer)) {
jTextPane1.setText(jTextPane1.getText() + "\n" + consoleOutput);
highlight();
}
jTextPane1.setText(jTextPane1.getText() + "\n[INFO] [BukkitUI] Server has Stopped!");
highlight();
stopServer();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
pr.destroy();
try {
consoleReader.close();
} catch (IOException ex1) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex1);
}
setStatusImg(ServerState.OFFLINE);
setTitleText("BukkitUI by Beatsleigher [ERROR! Server has stopped!]");
}
}
}.start();
}
服务器启动并运行正常,我得到了我需要的所有进程输出,但是,如前所述,我使用以下代码发送到控制台的任何命令都不起作用。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String inputCmd = jTextField1.getText();
try {
if (inputCmd != null | !inputCmd.equals("")) {
if (inputCmd.startsWith("/")) {
String[] cmd = inputCmd.split("/");
consoleWriter.write(cmd[1]);
clearCmd();
return;
} else if (inputCmd.startsWith("cmd;")) {
String[] cmd = inputCmd.split("cmd;");
consoleWriter.write(cmd[1]);
clearCmd();
return;
}
if (inputCmd.startsWith("bcast;")) {
String[] msg = inputCmd.split("bcast;");
consoleWriter.write("broadcast " + msg[1]);
clearCmd();
return;
}
if (inputCmd.startsWith("appnd;")) {
String[] msg = inputCmd.split("appnd;");
jTextPane1.setText(jTextPane1.getText() + "\n[BUKKITUI] " + msg[1]);
clearCmd();
return;
}
if (inputCmd.equals("--h")) { showHelp(); clearCmd(); return; }
consoleWriter.write("say " + inputCmd);
clearCmd();
}
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
非常感谢任何有关此事的帮助。