Title: Execute grep command on Linux and capture result
Tags: java;linux;jakarta-ee;grep
Question: 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?
Comment: @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.

Comment: 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…

Comment: 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)

Comment: @MarkoTopolnik - The accepted answer to the question I linked to is exactly what OP needs (regardless of the question itself).

Here is the accepted answer: 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();
Comment for this answer: @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.
Comment for this answer: 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.
Comment for this answer: Also make sure that your command prints output on shell. This command might not be returning anything.
Comment for this answer: Check [this link](http://www.mkyong.com/java/how-to-execute-shell-command-from-java/) for some examples. It should help.
Comment for this answer: 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 ?
Comment for this answer: 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 :(


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:09/15/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><linux><jakarta-ee><grep>', 'Title': 'Execute grep command on Linux and capture result', 'ViewCount': 1982, 'id': 25853102, 'user_AboutMe': '<p>A Tech Geek &amp; Internet nerd, loves to roam around the planet and become a universal citizen</p>\n', 'user_AccountId': 1126328.0, 'user_DisplayName': 'Dhruv', 'user_Reputation': 9745.0, 'user_Views': 728.0, 'user_id': 1113542.0}}