I am getting an exception like java.io.IOException: Cannot run program cat /home/talha/* | grep -c TEXT_TO_SEARCH": error=2, No such file or directory
while executing the command below despite that there are no issues when I execute the same command through the terminal. I need to execute and return the output of the command below:
cat /home/talha/* | grep -c TEXT_TO_SEARCH
Here is the method used to execute commands using Runtime
class:
public static String executeCommand(String command) {
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}
Runtime.exec does not use a shell (like, say,
/bin/bash
); it passes the command directly to the operating system. This means wildcards like*
and pipes (|
) will not be understood, sincecat
(like all Unix commands) does not do any parsing of those characters. You need to use something likeor, if for some bizarre reason you need to stick to using the obsolete Runtime.exec methods:
If you are only running that cat/grep command, you should consider abandoning the use of an external process, since Java code can easily traverse a directory, read lines from each file, and match them against a regular expression:
Update: To recursively read all files in a tree, use Files.walk:
$PATH
is an environment variable that tells the system where to search for executable programs (it's a list of directories separated by colons). It is usually set in your.bashrc
or.cshrc
file but this is only loaded when you log in. When Java runs,$PATH
is likely not set because therc
file is not executed automatically, so the system can't find programs without specifying exactly where they are. Try using/bin/cat
or/usr/bin/cat
instead of justcat
and see if it works. If it does,$PATH
is your problem. You can add$PATH=/bin:/usr/bin
to your script or just leave it with the directory name specified (e.g./bin/cat
).Just because you can execute it in a login session doesn't mean it will work the same when a daemon like your Java program runs. You have to know what's in your
.bashrc
or.cshrc
file and even sometimes how the system file is written (/etc/bashrc
) in order to know how to write a script that runs under a daemon. Another consideration is that daemons often run under the context of a different user, and that throws things off, too.