import java.io.File;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.*;
import java.io.InputStream;
class pbdemo {
static public void main(String[] args) throws Exception {
String s;
try {
ProcessBuilder pb = new ProcessBuilder("cmd", "/f", "dir");
pb.directory(new File("F:\\WINDOWS"));
pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(pb.getInputStream()));
while ((s = br.readLine()) != null)
System.out.println(s);
} catch (Exception e) {
System.out.println("sorry" + e);
}
}
}
what is the problem in
BufferedReader br=new BufferedReader(new InputStreamReader(pb.getInputStream()));
I get cannot find symbol error.
Simple answer.
ProcessBuilder
don't havegetInputStream
method.See this: ProcessBuilder
You can use:
You are trying to get an
InputStream
from aProcessBuilder
and that's wrong.ProcessBuilder.start()
returns a Process object, and that's the one who has agetInputStream()
method.Try switching that line for this one.
BufferedReader br=new BufferedReader(new InputStreamReader(pb.start().getInputStream()));
Or