执行adb命令报错:/system/bin/sh: findstr: not found

使用java执行adb命令出现报错:

    命令:adb shell dumpsys window w |findstr \/ |findstr name=

    报错内容:

        /system/bin/sh: findstr: not found
        /system/bin/sh: findstr: not found
        Failed to write while dumping service window: Broken pipe

    具体情况:

        命令是用来查询手机当前包名和应用名的,发现在cmd命令提示符中输入查询命令的话,可以查询出来内容,但是用java来执行adb命令就查不出来内容,并出现上述报错,java代码如下:

        String command = "adb shell dumpsys window w |findstr \\/ |findstr name=";
        try {
            Process p = Runtime.getRuntime().exec(command);
            String line = "";
            BufferedReader errorReader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            while ((line = errorReader.readLine()) != null) {  // 打印报错信息
                System.out.println("Err ==> " + line);
            }
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            while ((line = bufferedReader.readLine()) != null) {  // 打印执行结果
                System.out.println(line);
            }
            p.destroy();
        } catch (Exception e) {
            e.getStackTrace();
        }

    解决办法:

        看报错内容是findstr不能用,将findstr替换成grep就可以了,将查询命令替换为:

        String command = "adb shell dumpsys window w |grep \\/ |grep name=";

猜你喜欢

转载自blog.csdn.net/hxy199421/article/details/85065809