JavaFX:将 Input/Output/Error 从 ProcessBuilder 重定向到 TextArea?

JavaFX : Redirect Input/Output/Error From ProcessBuilder to TextArea?

我正在学习 JavaFX 并进行试验。我试图构建一个 Console/Command Prompt 。但我不知道如何将 Input/Output/Error 从 ProcessBuilder 重定向到 TextArea。

代码:

  public class JavaFXConsole extends Application {

 ProcessBuilder pb;
Process process;
TextArea ta;
String input = "";
int counter = 0;
//static BufferedReader stdInput, stdError;

//static Thread outputThread;
@Override
public void start(Stage primaryStage) {
    ta = new TextArea("$");
    ta.setOnKeyTyped(new EventHandler<KeyEvent>() {

        @Override
        public void handle(KeyEvent event) {
            {
                input = input.concat(event.getCharacter());
                System.out.println(input);
            }

        }
    });

    final KeyCombination keyComb1 = new KeyCodeCombination(KeyCode.C,
            KeyCombination.CONTROL_DOWN);
    ta.setOnKeyPressed(new EventHandler<KeyEvent>() {

        @Override
        public void handle(KeyEvent event) {
            if (event.getCode() == KeyCode.ENTER) {
                getOutPut(input);
                input = "";
                System.out.println("Command Sent");
            } else if (keyComb1.match(event)) {
                System.out.println("Control+C pressed");
            }
        }
    });

    HBox root = new HBox();
    root.getChildren().add(ta);

    pb = new ProcessBuilder("bash");
    try {
        process = pb.start();

    } catch (IOException ex) {
        Logger.getLogger(JavaFXConsole.class.getName()).log(Level.SEVERE, null, ex);
    }
    Scene scene = new Scene(root, 300, 250);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
}

public void getOutPut(String command) {

    Thread outputThread = new Thread(new Runnable() {

        @Override
        public void run() {

            try {
                BufferedReader stdInput, stdError;
                stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));

                stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));

                OutputStream out;
                out = process.getOutputStream();
                out.write(command.getBytes());
                //out.flush();
                out.close();
                // read the output from the command
                System.out.println("Here is the standard output of the command:\n");
                String s = null;
                String output = "";
                int ocounter = 0;
                while ((s = stdInput.readLine()) != null) {
                    ocounter++;
                    System.out.println(s);
                    String str = s;
                    Platform.runLater(new Runnable() {

                        @Override
                        public void run() {
                            ta.appendText(">"+str + "\n");
                        }
                    });

                }

                output = "";

                ocounter = 0;
                // read any errors from the attempted command
                System.out.println("Here is the standard error of the command (if any):\n");
                while ((s = stdError.readLine()) != null) {
                    ocounter++;
                    System.out.println(s);
                    String str = s;
                    Platform.runLater(new Runnable() {

                        @Override
                        public void run() {
                            ta.appendText("*"+str + "\n");
                        }
                    });

                }
                Platform.runLater(new Runnable() {

                    @Override
                    public void run() {
                        ta.appendText("$");
                    }
                });

                // stdError.close();
                // stdInput.close();
            } catch (IOException ex) {
                Logger.getLogger(JavaFXConsole.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    });

    outputThread.start();

}


/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

任何建议都会有所帮助。 :)

编辑:1

做了一点改进,但​​现在出现异常,只能执行一个命令

如果我不在 out.close(); 中关闭流。命令将不会执行。

   Jan 22, 2015 10:54:57 PM javafxconsole.JavaFXConsole handle
   SEVERE: null
java.io.IOException: Stream closed
at java.lang.ProcessBuilder$NullOutputStream.write(ProcessBuilder.java:433)
at java.io.OutputStream.write(OutputStream.java:116)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
at java.io.FilterOutputStream.close(FilterOutputStream.java:158)
at javafxconsole.JavaFXConsole.handle(JavaFXConsole.java:69)
at javafxconsole.JavaFXConsole.handle(JavaFXConsole.java:58)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)

at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$KeyHandler.process(Scene.java:3931)
at javafx.scene.Scene$KeyHandler.access00(Scene.java:3877)
at javafx.scene.Scene.impl_processKeyEvent(Scene.java:2006)
at javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2468)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:197)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:147)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:227)
at com.sun.glass.ui.View.handleKeyEvent(View.java:544)
at com.sun.glass.ui.View.notifyKey(View.java:954)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null(GtkApplication.java:126)
at com.sun.glass.ui.gtk.GtkApplication$$Lambda/379110473.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)

我解决了问题,不需要关闭流。仅在每个命令末尾附加 \n 并刷新流。特别感谢我的朋友 Julio Gessar,他在看了一眼代码后为我提供了解决方案。这是代码。我希望此代码可用于使用 Java 和 JavaFX 创建自定义终端或终端仿真器。感谢@colti 回答我的问题。

代码如下:

   /**
    *
    * @author nika
    */
    public class JavaFXConsole extends Application {

ProcessBuilder pb;
Process process;
TextArea ta;
String input = "";
int counter = 0;
BufferedReader stdInput, stdError;
OutputStream out;

//static BufferedReader stdInput, stdError;
//static Thread outputThread;
@Override
public void start(Stage primaryStage) throws InterruptedException {
    ta = new TextArea("/bin/bash\n");
    ta.setOnKeyTyped(new EventHandler<KeyEvent>() {

        @Override
        public void handle(KeyEvent event) {
            {

                input = input.concat(event.getCharacter());
                System.out.println(input);
            }

        }
    });

    final KeyCombination keyComb1 = new KeyCodeCombination(KeyCode.C,
            KeyCombination.CONTROL_DOWN);
    ta.setOnKeyPressed(new EventHandler<KeyEvent>() {

        @Override
        public void handle(KeyEvent event) {
            if (event.getCode() == KeyCode.ENTER) {
                try {
                    //getOutPut(input+"\n");
                    String command = input.replaceAll("\r", "") + "\n";
                    command = input.replaceAll("\b", "");
                    out.write(command.getBytes());
                    out.flush();
                    //getOutPut(command);
                    input = "";
                    System.out.println("Command Sent");
                } catch (IOException ex) {
                    Logger.getLogger(JavaFXConsole.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else if (keyComb1.match(event)) {

                System.out.println("Control+C pressed");
            }else if (event.getCode()== KeyCode.BACK_SPACE){
             //    input=input.substring(0, input.length()-1);
              //  System.out.println(input);
            }

        }
    });

    HBox root = new HBox();
    root.getChildren().add(ta);

    pb = new ProcessBuilder("/bin/bash");
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                process = pb.start();
                stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));

                stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));

                //OutputStream out;
                out = process.getOutputStream();

                StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "ERROR");

    // any output?
                StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream(), "OUTPUT");

    // start gobblers
                outputGobbler.start();
                errorGobbler.start();

                process.waitFor();

            } catch (IOException ex) {
                Logger.getLogger(JavaFXConsole.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InterruptedException ex) {
                Logger.getLogger(JavaFXConsole.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }).start();

    Scene scene = new Scene(root, 300, 250);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();

}


private class StreamGobbler extends Thread {

    InputStream is;
    String type;

    private StreamGobbler(InputStream is, String type) {
        this.is = is;
        this.type = type;
    }

    @Override
    public void run() {
        try {
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.println(type + "> " + line);
                String str =line;
                 Platform.runLater(new Runnable() {

                        @Override
                        public void run() {
                            ta.appendText(">" + str + "\n");

                        }
                    });
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}