0

I've searched quite a bit for answers, but haven't found anything that really helps, and the threads I found were zombies.

My problem: I'm creating a UI for Bukkit servers, but for end-users to execute commands in the server, I have to write to the process using an OutputStreamWriter. I have all the code, but the command isn't getting sent to the process.

So far, this is the code I'm using to execute the server and get all the streams:

@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();
}

The server starts and runs fine, I get all the process output I need, but, as mentioned before, any commands I send to the console using following code won't work.

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);
    } 

Any help with this matter is greatly appreciated.

Charles
  • 50,943
  • 13
  • 104
  • 142
SimonC
  • 1,547
  • 1
  • 19
  • 43
  • "I have to write to the process using an OutputStreamReader." <-- uh, you can't write to a `Reader` – fge Feb 15 '14 at 17:46
  • Oops. I mean writer. So used to writing reader: But it is a BufferedWriter(new OutputStreamWriter())) – SimonC Feb 15 '14 at 17:59
  • Are you sure the program you use accepts inputs from things other than terminals? For instance, have you tried to redirect the input of a process to a file with commands in them? – fge Feb 15 '14 at 18:08
  • Yes, I am sure. Normally the server is run within a terminal window and then commands are executed via command input. – SimonC Feb 15 '14 at 18:14
  • Well that's the thing: the program can detect that the standard input is a terminal and refuse to accept commands if it isn't; hence the test I asked you to do. If you are using Linux/Unix, you can even try using a named pipe – fge Feb 15 '14 at 18:17
  • I'm sure it shouldn't make a difference, both are Java programs. But I'll give it a try. – SimonC Feb 15 '14 at 18:54

0 Answers0