Arya
Arya

Reputation: 8995

Command working in terminal, but "no closing quote" error when used Process.exec

I have the following method inside a Java program

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();
    }
}

The output of the above method is

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

But if I copy/paste the command in terminal directly everything works as expected. Why am I getting the "no closing quote" error here?

Upvotes: 3

Views: 7988

Answers (1)

LMC
LMC

Reputation: 12875

try passing an array as suggested on javadocs.

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

Upvotes: 5

Related Questions