Output from command prompt returning null

别来无恙 提交于 2019-12-24 09:41:07

问题


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

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.


回答1:


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\"\""

First command process:

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.

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

Second command process:

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.

Third command process:

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.

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.

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.

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 is not really needed here as also dir which is completely useless.

I suggest to use:

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

which results on execution of

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

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

And read also the Microsoft article Using command redirection operators.




回答2:


The command you are executing is

cmd /c start cmd.exe /K \"cd c:/Window...

Notice you run cmd and ask it to run start, which creates a new (different) console session in which the subsequent command string is executed. The output from the start command is what you are reading, and start normally prints nothing, which is what you are reading.

Since you ran your command string in a new session/window, that is where the output you want is going. Omit the start altogether to have the output returned to your Java program.



来源:https://stackoverflow.com/questions/38113931/output-from-command-prompt-returning-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!