命令在终端工作,但"没有收尾报价"使用Process.exec时出错

时间:2018-05-29 20:16:42

标签: java linux

我在Java程序中有以下方法

public static void enableAirplaneMode() {
    String s = null;
    try {
        Runtime rt = Runtime.getRuntime();
        Process pr = null;
        String command = "adb shell \"su -c 'settings put global airplane_mode_on 1'\"";
        System.out.println(command);
        pr = rt.exec(command);

        BufferedReader stdInput = new BufferedReader(new InputStreamReader(pr.getInputStream()));

        while ((s = stdInput.readLine()) != null) {
            System.out.println(s);
        }

        pr.destroy();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

上述方法的输出是

adb shell "su -c 'settings put global airplane_mode_on 1'"
/system/bin/sh: no closing quote

但是如果我直接在终端中复制/粘贴命令,一切都按预期工作。为什么我得到"没有收尾报价"错误在这里?

1 个答案:

答案 0 :(得分:3)

按照javadocs上的建议尝试passing an array

String[] command = {"adb", "shell", "su -c 'settings put global airplane_mode_on 1'"};