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?

4
  • possible duplicate of How to execute system commands (linux/bsd) using Java Commented Sep 15, 2014 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. Commented Sep 15, 2014 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… Commented Sep 15, 2014 at 17:32
  • @MarkoTopolnik - The accepted answer to the question I linked to is exactly what OP needs (regardless of the question itself). Commented Sep 15, 2014 at 22:12

1 Answer 1

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

}
Sign up to request clarification or add additional context in comments.

6 Comments

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 ?
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.
Also make sure that your command prints output on shell. This command might not be returning anything.
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 :(
Check this link for some examples. It should help.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.