【问题标题】:Command working in terminal, but "no closing quote" error when used Process.exec命令在终端中工作,但使用 Process.exec 时出现“没有结束引号”错误
【发布时间】:2018-05-29 20:16:42
【问题描述】:

我在 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

但是,如果我直接在终端中复制/粘贴命令,一切都会按预期工作。为什么我在这里收到“没有结束引号”错误?

【问题讨论】:

标签: java linux


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-29
    • 1970-01-01
    • 2014-12-28
    • 1970-01-01
    • 2011-06-24
    • 2020-05-27
    相关资源
    最近更新 更多