简体   繁体   English

如何在java中获取命令提示符输出?

[英]How to get command prompt output in 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\"");

i can not get the cmd output like this我无法获得这样的 cmd 输出

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 ?那么我怎样才能得到命令提示符输出呢?

This is not Java question.这不是 Java 问题。 Basically what you doing is running Java (Java Main Process A) and from it starting another process (Windows CMD B).基本上你所做的是运行 Java(Java 主进程 A)并从中启动另一个进程(Windows CMD B)。 This is fine and you can get input/output streams of this process (B) in Java(A).这很好,您可以在 Java(A) 中获取此进程 (B) 的输入/输出流。 However this process (B) starts another process (again Windows CMD C) with its own standard input/output.但是,此进程 (B) 使用自己的标准输入/输出启动另一个进程(再次是 Windows CMD C)。 This process has nothing common with processes A&B and uses Windows' standard Input/Output streams.此进程与进程 A&B 没有任何共同之处,并使用 Windows 的标准输入/输出流。 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.所以,A 和 C 之间没有联系。我不确定,但我认为有一些方法可以使用不同的或非标准的 IO 运行 Windows CMD。 Maybe something like this will work:也许这样的事情会奏效:

cmd <tty >tty

but there is no tty in Windows.但是 Windows 中没有 tty。 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.实际上,您可以按照此处所述执行此操作 - 创建具有重定向输入和输出的子进程,但这不适用于常规 CMD。

Nevertheless it became even more problematic when you start your own process from the editorTemp.exe (process D).然而,当您从 editorTemp.exe(进程 D)启动自己的进程时,问题变得更加严重。 D has even more disconnection with process A. And all for what? D 与进程 A 有更多的断开。这一切都是为了什么? What don't you simply start process D directly from A and have full control on the IO streams and process itself?你不是简单地直接从 A 启动进程 D 并完全控制 IO 流和进程本身吗? Here is good example how to do so.这是如何做到这一点的好例子

Your java thread is working independently of CMD call.您的 java 线程独立于 CMD 调用工作。 The java code is beating the STDOUT pipe before anything is written.在写入任何内容之前,java 代码正在击败STDOUT管道。

If you call Process.waitFor() , it will wait until the CMD call is done.如果您调用Process.waitFor() ,它将一直等到 CMD 调用完成。 The STDOUT should be in the buffer, and then you can read it. STDOUT应该在缓冲区中,然后您可以读取它。

When you do a readLine(), your java thread is blocked until you have an actual full line or the input stream is closed.当您执行 readLine() 时,您的 java 线程将被阻塞,直到您有实际的完整行或输入流关闭。

If the program prints a partial line (without CR or LF at the end), and then waits for input, the readLine will be stuck.如果程序打印了部分行(末尾没有 CR 或 LF),然后等待输入,则 readLine 会卡住。

So you will need to read character by character, until you think the proces has no more things to say.因此,您需要逐个字符地阅读,直到您认为该过程无话可说。

See eg Is it possible to read from a InputStream with a timeout?参见例如是否可以从带有超时的 InputStream 读取?

import java.util.Scanner;

Inside the main write this.里面主要写这个。

Scanner output = new Scanner(System.in);

System.out.println(“Enter your name”);
String name = output.next();

If you want user to enter an int then you need to do this.如果您希望用户输入 int,则需要执行此操作。

int number = output.nextInt();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Java中获取命令提示输出到字符串 - Get Command Prompt Output to String In Java 输入命令并获取命令提示符完整输出到Java中的textarea - Give input command and Get Command Prompt complete Output to textarea In Java 如何在Windows命令提示符下使用单个命令获取java版本? - How to get java version using single command in Windows command prompt? 使用java将输出写入命令提示符 - write output to command prompt using java 如何在Java中将AT命令的输出转换为字符串? - How to get the output of AT command into a string in java? 如何通过Java执行命令提示符命令 - How to execute command prompt command through java 命令提示符和输出文件之间的Java拆分输出 - Java-split output between command prompt and output file 如何通过Windows命令提示符下的JScript收集运行Java小程序的输出? - How to collect output from running Java applet via JScript in Windows command prompt? 如何使用Java // notepad在命令提示符下在控制台屏幕/输出上显示多行内容 - how to display anything multi line as it is on console screen/output in this case on command prompt using java //notepad 如何使用语言Java从命令提示符中获取输出并使用文本制作文本文件 - how to take the output from command prompt and make a text file out of it using language Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM