捕获DOS的输出(命令提示符)并显示在JAVA框架中 [英] Capture the output of DOS (command prompt) and display in a JAVA Frame

查看:136
本文介绍了捕获DOS的输出(命令提示符)并显示在JAVA框架中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行python脚本时,输出显示在DOS上(Windows中的命令提示符)。

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

我希望输出显示在JAVA应用程序上,即在一个包含JTextArea的窗口上。输出应该与DOS上的输出相同。

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.

那么,如何从DOS捕获输出并将其插入JAVA应用程序?

(我尝试将python脚本的输出存储在文本文件中,然后使用JAVA读取它。但是,在这种情况下,JAVA应用程序等待脚本先完成运行然后显示输出。 ,当输出超过屏幕尺寸时,滚动条会出现,这样我就可以看到整个输出。)

(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 ??

推荐答案

您可以通过 ProcessBuilder ,它将为您提供 处理 实例您可以通过 getInputStream

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.

以下是运行Python脚本的示例 hello.py 并以字符串形式建立其输出:

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

然后你可以用字符串做任何你喜欢的事情,包括像任何其他字符串一样将它放在 JTextArea 中。 (如果你愿意的话,可以在 InputStreamReader 周围使用 BufferedReader ,但是你明白了。)

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.)

这篇关于捕获DOS的输出(命令提示符)并显示在JAVA框架中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆