I am running grep command via my Java program. Running grep on the command line sometimes writes an error on stderr of the kind: No such file or directory. I want to detect in my Java program whenever this error happens as a result of executing the grep command via the program. How can I achieve this goal of mine? This is what I've written so far:
Runtime rt = Runtime.getRuntime();
String[] cmd = {"/bin/sh", "-c", "grep -c 'Search_String' /path/to/file(s)/being/searched"};
Process proc = rt.exec(cmd);
BufferedReader is = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
String line;
while ((line = is.readLine()) != null) {
System.out.println(line);
}