繁体   English   中英

从Java在Linux上执行grep命令总是返回null

[英]Executing grep command on Linux from Java always returning null

我正在Linux文件上从Java执行grep命令。 对于以下代码,它始终返回null。

Process p;
String matchStr="testmatch";
String output = null;
try {
    String command = "grep \""+matchStr+"\" "+ filename;
    System.out.println("Running command: " + command);

    p = Runtime.getRuntime().exec(command);

    System.out.println("***********************************");
    System.out.println("***********************************");
    System.out.println("***********************************");

    p.waitFor();
    BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

    while (br.readLine() != null) {
        System.out.println("in while loop");
        System.out.println("in while loop");
        System.out.println("in while loop");
        System.out.println(output);
        System.out.println("***********************************");
        System.out.println("***********************************");
        System.out.println("***********************************");
        System.out.println("***********************************");

        // Process your output here
    }

    System.out.println("exit: " + p.exitValue());
    p.destroy();
} catch (Exception e) {
    e.printStackTrace();
}

如果我直接使用grep,它将显示输出,但是从java不会进入while循环。 请在这里提出问题。

问题是您不编写任何要output内容,因此它保持为null 我猜你必须像这样重写while循环

while ((output = br.readLine()) != null) {
    // Process your output here
}

请注意,大多数语法检查不建议使用此语法,因为它过于含糊

最好在while循环后放置p.waitFor() ,这样grep不会挂在flushig std(err|out)

UPDATE

另外,最好使用ProcessBuilder (从可用)而不是Runtime.getRuntime().exec(...)因为您将对进程有更多控制,即

final ProcessBuilder builder = new ProcessBuilder();
builder.command("grep", matchStr, filename);

// redirect stderr to stdout
builder.redirectErrorStream(true);

final Process process = builder.start();

BufferedReader br = new BufferedReader(
    new InputStreamReader(process.getInputStream()));
String output = null;
while ((output = br.readLine()) != null) {
    System.out.println(output);

    // Process your output here
}

process.waitFor();

将您的代码转换为https://stackoverflow.com/help/mcve后,它对我有用。

此处文件不存在:

robert @ habanero:〜$ rm /home/robert/greptest.txt

robert @ habanero:〜$ javac GrepTest.java && java GrepTest

运行命令:grep test /home/robert/greptest.txt

出口:2

现在,该文件确实存在,但不包含要查找的文本:

robert @ habanero:〜$找不到回显> /home/robert/greptest.txt

robert @ habanero:〜$ javac GrepTest.java && java GrepTest

运行命令:grep test /home/robert/greptest.txt

出口:1

现在该文件已存在并包含以下文本:

robert @ habanero:〜$ echo测试此> /home/robert/greptest.txt

robert @ habanero:〜$ javac GrepTest.java && java GrepTest

运行命令:grep test /home/robert/greptest.txt

测试这个

出口:0

这是代码:

import java.io.*;

public class GrepTest {
    public static void main(String[] args) throws Exception {
        String command = "grep test /home/robert/greptest.txt";
        System.out.println("Running command: " + command);
        Process p = Runtime.getRuntime().exec(command);
        p.waitFor();
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String output;
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }
        System.out.println("exit: " + p.exitValue());
        p.destroy();
    }
}

我最近正在努力解决类似的问题,我相信我找到的解决方案也可以解决您的问题(尽管您的问题有点畸形,正如其他人指出的那样)。

这个问题会影响您搜索字符串周围的引号,

\""+matchStr+"\"

java exec命令将从字面上将这些命令传递给grep命令,并且grep会搜索“ matchStr”,而不是搜索matchStr,结果将不是您期望的。

这也适用于将命令作为数组执行的情况,例如

final Process process = Runtime.getRuntime().exec(new String[] { "grep", "-C1000", searchString, fileName } );

将普通的searchString传递给字符串,但不包括引号。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM