Skip to content Skip to sidebar Skip to footer

Not Able To Run Grep Command

I was trying to run the following instruction, Process p = Runtime.getRuntime().exec('/system/bin/lsof|grep mediaserver'); In android(java) but I am getting error. if I run follo

Solution 1:

The grep utility may not be installed on your device.

You can check it by trying the following in a console:

> adb shell
$ grepgrep: not found

The last line indicates that this command is not available.

Solution 2:

The problem is that Runtime.getRuntime().exec(...) does not know how to deal with the shell language. On a Linux / Unix platform you would so something like this:

Processp= Runtime.getRuntime().exec(newString[]{
    "/bin/sh", "-c", "/system/bin/lsof | grep mediaserver"});

However, (apparently) Android doesn't have a command shell / command prompt by default. So either you need to identify and install a suitable shell on your device, or construct the pipeline "by hand"; i.e. by creating a "pipe" file descriptor, and execing the two commands so that lsof writes to the pipe and grep reads from it.

Maybe the answer is to run it like this ...

Process p = Runtime.getRuntime().exec(
        "adb shell /system/bin/lsof | grep mediaserver");

(Try running the "shell ...." part from adb interactively before doing it from Java.)

Post a Comment for "Not Able To Run Grep Command"