0

I am trying to execute grep command on the linux using the following java code, but I am not able to capture output. I am getting always null in the output

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();

    }

How to capture the output? Is there any third party library or some better way to execute the command on linux and capture the output?

Dhruv
  • 10,291
  • 18
  • 77
  • 126
  • possible duplicate of [How to execute system commands (linux/bsd) using Java](http://stackoverflow.com/questions/792024/how-to-execute-system-commands-linux-bsd-using-java) – PM 77-1 Sep 15 '14 at 17:04
  • @PM77-1 Next time please spend a second to read the question before marking as a duplicate. OP's problem has nothing to do with executing system commands. – Marko Topolnik Sep 15 '14 at 17:11
  • You are saying `if (null != output) {` before assigning any value (other than `null`) to the variable `output`. Therefore, since `output` *is* `null` at this point you never invoke `readLine`. If you use an IDE like Eclipse it will tell you something like “this variable can only be `null` at this point” at you `if` statement… – Holger Sep 15 '14 at 17:32
  • @MarkoTopolnik - The accepted answer to the question I linked to is exactly what OP needs (regardless of the question itself). – PM 77-1 Sep 15 '14 at 22:12

1 Answers1

1

output has not been assigned when you do if check. Change your code like below:

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

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

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

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

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

}
oardic
  • 212
  • 1
  • 3
  • sorry didn't got u correctly.. actually earlier my code have some business logic code also..which I removed now.. but after removing I guess. ur code and my code exactly looks the same... kindly explain if i am missing something... have to tried executing this code of urs ? – Dhruv Sep 15 '14 at 17:20
  • No i haven't tried it. But it's quite clear that if block cannot be put there. Except that try removing `p.destroy()` and close `BufferedReader` instance after processing. – oardic Sep 15 '14 at 17:32
  • Also make sure that your command prints output on shell. This command might not be returning anything. – oardic Sep 15 '14 at 17:40
  • yes... i made a sysout of command before executing and when I run the same command..its printing output on shell but not with java program... not sure whats going wrong :( – Dhruv Sep 15 '14 at 17:43
  • Check [this link](http://www.mkyong.com/java/how-to-execute-shell-command-from-java/) for some examples. It should help. – oardic Sep 15 '14 at 17:47
  • @oardic the `waitFor` method should be used after the while loop. There could be deadlocks if you do not read from stdout/stderr streams. Anyway, in this simple case I do not think such thing could happen. – Gooseman Sep 15 '14 at 18:55