1

I'm trying to read output from command.com not cmd.exe, so I've tried to go with this code:

    Process p=Runtime.getRuntime().exec("command /c echo goodbye world"); //command.com doesn't work eiher
    p.waitFor(); 
    BufferedReader reader=new BufferedReader(
        new InputStreamReader(p.getInputStream())
        ); 
    String line=""; 
    String output="";
    while((line = reader.readLine()) != null) 
        { 
        output+=line;
        }
    System.out.print(line);

Unfortunately, it doesn't print anything, but it works when I replace command with cmd perfectly. I've tried ro run 'command /c echo goodbye world' in cmd, and it works. I've tried this code in windows me and XP, because command.com doesn't exist in new windows versions.

user3379482
  • 557
  • 1
  • 10
  • 21

2 Answers2

0

You could try modifying the command command to send its output to file, then after p.waitFor() finishes, you could read the input from that file.

Process p=Runtime.getRuntime().exec("command /C echo goodbye world > output.txt");

It's a guess, but maybe the command.com interpreter is being case-sensitive on the /C switch. Maybe try it with upper-C instead and see what happens. Also you can use ProcessBuilder instead of Process because it will let you redirect stderr of the command to stdout so that you can gather them both from your inputstream.

b4n4n4p4nd4
  • 70
  • 1
  • 10
  • Thanks for your answer but as I said, I'm using command.com not cmd.exe, and I've tried to redirect the output of command.com using this code with no luck. – user3379482 Oct 31 '16 at 19:41
  • Revised exec for command instead of cmd. Changed /c to /C. Also a possible problem is that you're trying to read off the inputstream of a command program which would only capture things like keyboard or maybe file redirection input. If you want to grab what the command.com prints out that isn't from the keyboard, I think you want to grab a handle to the output stream of the process. – b4n4n4p4nd4 Nov 01 '16 at 15:35
  • Please ignore previous about inputstream vs. outputstream. I had them backwards after reading the javadoc on process. Updated my answer to suggest using upper-C instead (maybe it's being case-sensitive). – b4n4n4p4nd4 Nov 01 '16 at 16:11
  • Sorry, it's not about upper case, tried it but didn't work -as I've expected- because -as I've said- it's working perfectly when I try it 'command /c echo goodbye world' in cmd instead of java – user3379482 Nov 02 '16 at 20:23
0

Try also capturing the error output and the exit code - if you're not seeing anything in the standard output it's likely there's an error message in the error stream. If that too is empty it's possible the exit code provides some additional debugging details. You can consult a Windows exit code table to try to determine the cause of the exit.

My guess is that for some reason the JVM cannot invoke command the same way it can invoke cmd (and the error stream contains a message to this effect). Perhaps command is not on the JVM's PATH or is not actually invokable from the system's shell. You could also try specifying the full path to the command.com executable.


As an aside, you should always use the mutable StringBuilder when composing strings like you're doing; it uses much less memory. Even better let a library do the work for you.

Community
  • 1
  • 1
dimo414
  • 47,227
  • 18
  • 148
  • 244