7

I am trying to read in the results of a cmd command (dir for example). After creating the process, I use a BufferedReader in conjunction with an InputStreamReader. For some reason, the BufferedReader keeps coming up empty, even though I know that there must be some output to be read.

Here is the code I'm using:

String[] str = new String[] {"cmd.exe", "/c", 
            "cd", "c:\\",
            "dir", "/b", "/s"               
    };
    Runtime rt = Runtime.getRuntime();
    try{

        Process p = rt.exec(str);
        InputStream is =p.getInputStream();
        System.out.println(is.available());
        InputStreamReader in = new InputStreamReader(is);

        StringBuffer sb = new StringBuffer();
        BufferedReader buff = new BufferedReader(in);
        String line = buff.readLine();
        System.out.println(line);
        while( line != null )
        {
            sb.append(line + "\n");
        System.out.println(line);
            line = buff.readLine();
        }
        System.out.println( sb );
        if ( sb.length() != 0 ){
            File f = new File("test.txt");
            FileOutputStream fos = new FileOutputStream(f);
            fos.write(sb.toString().getBytes());

            fos.close();
        }
    }catch( Exception ex )
    {
        ex.printStackTrace();
    }
Anthony Forloney
  • 90,123
  • 14
  • 117
  • 115
chama
  • 5,973
  • 14
  • 61
  • 77
  • is process.getErrorStream() also returning empty? – b.roth Feb 04 '10 at 16:57
  • yes - both the ErrorStream and InputStream have 0 bytes available – chama Feb 04 '10 at 17:01
  • I just ran it again, and the error stream is not empty. When I read the error stream, it printed "The system cannot find the path specified," which doesn't exactly make sense, but at least it's something. – chama Feb 04 '10 at 17:05
  • Try to force the physical address of the file, `C:\\test.txt` and see what happens – Anthony Forloney Feb 04 '10 at 17:07
  • Well it could mean that cmd.exe is not in the PATH (or %PATH%, or whatever it's called on windows). Can you try using the full path to the executable, at least for a start ? – phtrivier Feb 04 '10 at 17:08
  • I tried using cmd.exe before, and it worked, so I don't think it's a problem with the %PATH% variable. – chama Feb 04 '10 at 17:13

3 Answers3

5

You've got:

String[] str = new String[] {"cmd.exe", "/c", 
            "cd", "c:\\",
            "dir", "/b", "/s"               
    };

which doesn't seem right to me. You can't put multiple commands to cmd.exe on one command line. Thats a batch file.

Try getting rid of everything either the cd or the dir.

edit: indeed:

C:\>cmd.exe /c cd c:\ dir
The system cannot find the path specified.
Trevor Harrison
  • 1,744
  • 1
  • 14
  • 20
  • That solved the problem! I put an "&" between the cd command and the dir command and got the correct output! – chama Feb 04 '10 at 17:20
  • 2
    @chama: What about simply using `dir /b /s C:\​`? Just for the fun of it: Try running your code from another drive, such as `D:`. Your `cd` command would do nothing there. Generally, use whatever is applicable and don't try to overcomplicate things. This is an example where you need exactly one command which will do what you want. That sequence of two won't. – Joey Feb 04 '10 at 18:03
1

There could be an error. In this case you should also trap getErrorStream()

Chris Kannon
  • 5,931
  • 4
  • 25
  • 35
  • I tried trapping the ErrorStream and got "The system cannot find the path specified." But how can it not find c:\? – chama Feb 04 '10 at 17:12
1

The command you are running is cmd.exe /c cd c:\ dir /b /s. I don't think that's doing what you expect.


I mean that you have concatenated two commands into one line and the Windows shell probably doesn't like that. Try something like

String[] str = new String[] {"cmd.exe", "/c", 
            "cd", "c:\\", "&&",
            "dir", "/b", "/s"               
    };

The && will tell the shell to execute cd c:\ and then to execute dir /b /s if the first command was successful.

mob
  • 117,087
  • 18
  • 149
  • 283
  • that's a very good point. I was originally searching for a certain file in that folder. Unfortunately, taking out the /s didn't solve the problem – chama Feb 04 '10 at 17:18