1
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.

Michael Markidis
  • 4,163
  • 1
  • 14
  • 21
Kaustav Basu
  • 25
  • 1
  • 1
  • 8
  • Are you saying that nothing is output by the System.out.println(output) because the loop contents never runs because the line is read as null? Or are you saying the println prints out "null"? – David Findlay Jun 30 '16 at 04:26
  • @DavidFindlay yes it prints out nothing since output is null. – Kaustav Basu Jun 30 '16 at 04:33
  • I found multiple methods on how to launch your `dir` and `netstat` command inside a command prompt, but nothing which showed how to redirect the output coming from there back into Java. – Tim Biegeleisen Jun 30 '16 at 04:37
  • @TimBiegeleisen I wanted to see the output to confirm that the ldap connection was succesfull,as the above command gives out coonection established.Any way i can check if the connection was succesfull – Kaustav Basu Jun 30 '16 at 04:44
  • @KaustavBasu I know what you're trying to do, but with the methods I tested this doesn't seem possible. The `dir` command runs before the code even connects to the input stream. I hope a guru proves me wrong though. – Tim Biegeleisen Jun 30 '16 at 04:45
  • are you getting anything after executing the command in cmd prompt manually? – Saravana Jun 30 '16 at 04:48
  • I find it better to use `ProcessBuilder` to direct the out and error streams to one stream that you can print. – ifly6 Jun 30 '16 at 05:00
  • @Saravana Yes i am getting as connection established if i am doing manually.need to capture that and get it in the code,any suggestions – Kaustav Basu Jun 30 '16 at 05:09

2 Answers2

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.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143
0

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.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190