我试图用下面的java代码在linux上执行grep命令,但我无法捕捉到输出。我在输出中得到的总是空
Process p;
String output = null;
try {
String command = "grep searchString filename.txt";
System.out.println("Running command: " + command);
p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
if (null != output) {
while ((output = br.readLine()) != null)
System.out.println(output);
p.waitFor();
System.out.println("exit: " + p.exitValue());
p.destroy();
} catch (Exception e) {
e.printStackTrace();
如何捕获输出?
是否有任何第三方库或一些更好的方法来在linux上执行命令并捕获输出?