Capture the output of DOS (command prompt) and dis

2019-04-11 11:45发布

问题:

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

I want the output be displayed on a JAVA Application, i.e. on a window which contians JTextArea. The output should be same as that on the DOS.

So, How do I capture the output from the DOS and insert it into JAVA Application ??

(I tried storing output of the python script in a text file and then reading it using JAVA. But, in that case, the JAVA application waits for the script to finish running first and then displays the output. And, when the output is more than the screen size, a scroll bar appeears, so that I can see the entire output.)


After crowder's comment, I ran this code. but the output is always:

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

Can anyone tell me what am I doing wrong ??

回答1:

You can execute the process via a ProcessBuilder, which will give you a Process instance on which you can read the output via the stream returned from getInputStream.

Here's an example that runs the Python script hello.py and builds up its output in a string:

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

You can then do whatever you like with the string, including putting it in the JTextArea like any other string. (You could use a BufferedReader around the InputStreamReader if you like, but you get the idea.)



回答2:

You can connect to the error and inputStream, while Using Runtime.exec() oder ProcessBuilder.

Examples can be found here: http://www.java-tips.org/java-se-tips/java.util/from-runtime.exec-to-processbuilder.html



标签: java capture