Capturing DOS (command line) output and displaying in a JAVA frame

When I run the python script, the output appears in DOS (command line on Windows).

I want the output to be displayed in a JAVA application, i.e. in the window that corresponds to JTextArea. The output should be the same as on DOS.

So, How can I make a conclusion from DOS and paste it into a JAVA application?

(I tried to save the output of the python script in a text file and then read it using JAVA. But in this case, the JAVA application expects the script to finish first and then display the output. And, when the output is larger than the screen size, the scroll bar fits, so I can see the whole output.)


After re-commenting, I ran this code. but the output is always:

error: The process said:

import java.io.*;
import java.lang.*;
import java.util.*;
class useGobbler {
        public static void main ( String args[] )
        {
        ProcessBuilder pb; 
        Process p;
        Reader r;
        StringBuilder sb;
        int ch;

        sb = new StringBuilder(2000);

        try
        {
            pb = new ProcessBuilder("python","printHello.py");
            p = pb.start();

            r = new InputStreamReader(p.getInputStream());
            while((ch =r.read() ) != -1)
            {
                sb.append((char)ch);
            }

        }
        catch(IOException e)
        {
            System.out.println("error");

        }

        System.out.println("Process said:" + sb);
    }
}

- , ?

+3
2

ProcessBuilder, Process, , getInputStream.

, Python script hello.py :

import java.io.Reader;
import java.io.IOException;
import java.io.InputStreamReader;

public class RunPython {
    public static final void main(String[] args) {
        ProcessBuilder  pb;
        Process         p;
        Reader          r;
        StringBuilder   sb;
        int             ch;

        // Start the process, build up its output in a string
        sb = new StringBuilder(2000);
        try {
            // Create the process and start it
            pb = new ProcessBuilder("python", "hello.py");
            p = pb.start();

            // Get access to its output
            r = new InputStreamReader(p.getInputStream());

            // Read until we run out of output
            while ((ch = r.read()) != -1) {
                sb.append((char)ch);
            }
        }
        catch (IOException ex) {
            // Handle the I/O exception appropriately.
            // Here I just dump it out, which is not appropriate
            // for real apps.
            System.err.println("Exception: " + ex.getMessage());
            System.exit(-1);
        }

        // Do what you want with the string; in this case, I'll output
        // it to stdout
        System.out.println("Process said: " + sb);
    }
}

, , , JTextArea, . ( BufferedReader InputStreamReader, , .)

+3

All Articles