命令提示符的输出返回null [英] Output from command prompt returning null

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

问题描述

String line;
String output = "";
Process p = Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"cd c:/Windows/system32 && dir && netstat | Findstr \"ldap\"\"");
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
    output += (line + '\n');
}
System.out.println(output);
input.close();

这段代码对于许多人来说似乎是完美的,但对我来说input.readline只返回null。我猜是因为已经到达控制台的末端。我如何阅读上面的输出。任何帮助将不胜感激。

This code seems perfect for many,but for me input.readline just returns null.I guess because it has reached end of the console.How do i read the output above.Any help would be greatly appreciated.

推荐答案

您是否曾经想到过此行的作用,或者是否至少有一个人在命令提示符窗口中执行过此行删除转义的反斜杠后?

Do you have ever thought what this line does or do you have at least ones executed this line from within a command prompt window after removing the escaping backslashes?

"cmd /c start cmd.exe /K \"cd c:/Windows/system32 && dir && netstat | Findstr \"ldap\"\""

第一个命令进程:

cmd / c 启动一个新的Windows命令过程,当所有命令完成时,该过程将自动关闭。该命令过程只有一个命令要处理- start -成功后不会输出任何内容。

cmd /c starts a new Windows command process which closes automatically when all commands finished. There is only one command to process by this command process - start - which does not output anything on success.

这就解释了为什么没有从Java应用程序中开始的命令过程中捕获到任何输出行。

This explains why you get no output lines captured from this command process started from within the Java application.

第二个命令过程:

start 启动一个新的Windows命令过程以执行命令。要执行的命令是: cmd / K

执行此命令后,将关闭命令进程,就像使用 cmd / c

start starts a new Windows command process to execute a command. The command to execute is: cmd /K
After this command is executed the command process is closed like when using cmd /c.

第三命令过程:

cmd / K 使用当前(第二个)Windows命令进程的命令提示符窗口启动新命令进程,以执行包含多个命令的命令行,并在之后保持打开状态

cmd /K starts a new command process using the command prompt window of current (second) Windows command process to execute a command line with multiple commands and keep the command prompt window open after command line execution finished.

第二个命令提示符窗口中的第三个命令进程确实输出了行,但是Java应用程序未捕获此命令进程的输出。

This third command process in second command prompt window really outputs lines, but the output of this command process is not captured by the Java application.

有必要使用命令 exit 终止第三个命令过程,导致关闭已经打开的命令提示符窗口。第二个命令过程以命令 start 开始。

It would be necessary to use command exit to terminate the third command process resulting in closing the command prompt window opened already with second command process started with command start.

因为操作员是&& 而不仅仅是& ,仅当命令 cd 成功执行后,才会执行命令 dir 。有关& 使用Windows批处理文件的单行多个命令。 c>和& 。如果当前驱动器与驱动器不在同一驱动器上,则不带参数 / D 的命令 cd 无法更改当前目录。将目录驱动器设置为新的当前目录。

Because the operator is && and not just &, the command dir is executed only if command cd before was successful. See Single line with multiple commands using Windows batch file for more details about & and &&. The command cd without parameter /D fails to change the current directory if the current drive is on a different drive than the drive of the directory to set as new current directory.

cd 此处实际上并不需要,因为 dir 完全没用。

cd is not really needed here as also dir which is completely useless.

我建议使用:

"cmd.exe /C \"%SystemRoot%\\system32\\netstat.exe | %SystemRoot%\\system32\\findstr.exe /I /C:ldap\""

在执行后出现

cmd.exe /C "%SystemRoot%\system32\netstat.exe | %SystemRoot%\system32\findstr.exe /I /C:ldap"

无行如果 netstat 不输出任何行到 STDOUT ,而在任何行中包含字符串 ldap ,则输出/捕获案例重定向到 findstr STDIN ,在这里作为过滤器。

No line is output/captured if netstat outputs no line to STDOUT containing the string ldap in any case redirected to STDIN of findstr working here as filter.

命令及其工作方式,打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面。

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.


  • cd /?

  • cmd /?

  • dir /?

  • findstr /?

  • netstat /?

  • start /?

  • cd /?
  • cmd /?
  • dir /?
  • findstr /?
  • netstat /?
  • start /?

然后阅读Microsoft文章使用命令重定向运算符

And read also the Microsoft article Using command redirection operators.

这篇关于命令提示符的输出返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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