如何在Java中获取命令提示符输出? [英] How to get command prompt output in java?

查看:100
本文介绍了如何在Java中获取命令提示符输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取命令提示符的输出,这意味着我已经打开了这样的命令提示符。

How to get the output of the command prompt which means i have opend a command prompt like this.

Process process = Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"C:\\Editor\\editorTemp.exe\"");

我无法获得像这样的cmd输出

i can not get the cmd output like this

BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));

        String line = null;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }

那么我如何获得命令提示符输出?

So how can i get the command prompt output ?

推荐答案

这不是Java问题。基本上,您正在执行的是Java(Java Main Process A),然后从中启动另一个进程(Windows CMD B)。很好,您可以在Java(A)中获得此过程(B)的输入/输出流。
但是,此过程(B)使用其自己的标准输入/输出启动另一个过程(再次是Windows CMD C)。该过程与过程A& B没有共同之处,并使用Windows的标准输入/输出流。因此,A和C之间没有任何连接。
我不确定,但是我认为有一些方法可以使用不同的标准IO或非标准IO运行Windows CMD。也许这样的事情会起作用:

This is not Java question. Basically what you doing is running Java (Java Main Process A) and from it starting another process (Windows CMD B). This is fine and you can get input/output streams of this process (B) in Java(A). However this process (B) starts another process (again Windows CMD C) with its own standard input/output. This process has nothing common with processes A&B and uses Windows' standard Input/Output streams. So, there are no connections between A and C. I'm not sure but I think there are some ways to run Windows CMD with different or not standard IO. Maybe something like this will work:

cmd <tty >tty

,但是Windows中没有tty。实用上,您可以按照在这里-创建具有重定向的输入和输出的子进程,但不适用于常规CMD。

but there is no tty in Windows. Pragmatically you can do this as described here - Creating a Child Process with Redirected Input and Output but that would not work for regular CMD.

尽管如此,当您启动自己的子进程时,问题甚至更加严重来自editorTemp.exe的进程(进程D)。 D与进程A的连接甚至更多。所有的目的是什么?您是不是直接从A直接启动进程D,并完全控制IO流和进程本身,那又是什么呢?
这是一个很好的示例方法。

Nevertheless it became even more problematic when you start your own process from the editorTemp.exe (process D). D has even more disconnection with process A. And all for what? What don't you simply start process D directly from A and have full control on the IO streams and process itself? Here is good example how to do so.

这篇关于如何在Java中获取命令提示符输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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