[prev in list] [next in list] [prev in thread] [next in thread] 

List:       openjdk-openjfx-dev
Subject:    RE: How to redirect input/output/error from Process Builder To TextArea
From:       Navdeep Singh Sidhu <navdeepsingh.sidhu95 () gmail ! com>
Date:       2015-01-22 18:15:30
Message-ID: CACn=thZjeDPrhTjFqiAsT7wjHhJeMmcNgAhEj9tMCxgmM3ZUiA () mail ! gmail ! com
[Download RAW message or body]

I updated the code slightly. But now getting Stream is closed exception.
Without Closing "out" command doesn't executes.

Here is the code.

public class JavaFXConsole extends Application {

    ProcessBuilder pb;
    Process process;
    TextArea ta;
    String input = "";
    static OutputStream out;
    //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) {
                    try {

                        out = process.getOutputStream();
                        getOutPut();
                        out.write(input.getBytes());
                        input = "";
                        //out.flush();
                        out.close();
                        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");
                }
            }
        });

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

        pb = new ProcessBuilder("bash");
        //pb.inheritIO();
        try {
            process = pb.start();
        } catch (IOException ex) {

Logger.getLogger(JavaFXConsole.class.getName()).log(Level.SEVERE, null, ex);
        }

        // outputThread.start();
        Scene scene = new Scene(root, 300, 250);

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

    public void getOutPut() {

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

                    // read the output from the command
                    System.out.println("Here is the standard output of the
command:\n");

                    String s = null;
                    String output = "";
                    while ((s = stdInput.readLine()) != null) {
                        System.out.println(s);
                        String str = s;
                        Platform.runLater(new Runnable() {

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

                    }

                    output = "";

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

}


Anybody know to solve the problem?


Regards
Navdeep Singh Sidhu
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic