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.