Print the output of another program into the java text area

I am creating a GUI using Java. This GUI launches the program from the command line using the ProcessBuilder class.

A little information about the running process: from the command line, it creates another window and prints information to the specified window.

In my GUI window, I have a text area where I would like to redirect the specified output. Initially, I planned to use the SwingWorker object to constantly monitor a large amount of output and not delay the graphical interface. To check and make sure that I had the original syntax (not even a GUI added to things), I thought that I would print the output from the secondary process window to System.out. However, something seems to be wrong, as I see the output in the secondary process window, but not the terminal I'm working with.

Code highlighting is as follows:

Process p = pb.start(); 
Scanner s = new Scanner(p.getInputStream());

SwingWorker pipe = new SwingWorker<String, Void> (){
    public String doInBackground(){
        while(run){
            if(s.hasNextLine()){
                System.out.println("S has next!");
                System.out.println(s.nextLine());
            }
        }
        return null;
    }
};
pipe.execute();

The logical run is defined elsewhere in the program and is set to false when the process p quits or forcibly terminates (additional question: is this a really bad idea? I feel it could be ...).

- , , , ? p.getOutputStream(), Scanner outputStream .

.

+3
2

p.getErrorStream() - STDERR, STDOUT . , , , .

+3

, STDOUT, . , , , ( ). , , (.. , STDOUT ).

p.getOutputStream(), , "" , .. , "STDIN". p.getInputStream() , STDOUT.

+2
source

Source: https://habr.com/ru/post/1727391/


All Articles