3

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?

Arya
  • 8,473
  • 27
  • 105
  • 175
  • try [passing an array](https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String[])) `String[] command = {"adb", "shell", "su -c 'settings put global airplane_mode_on 1'"};`. – LMC May 29 '18 at 20:28
  • That works, you may post it as an answer and I will accept it – Arya May 29 '18 at 20:33
  • Dupe https://stackoverflow.com/questions/31776546/why-does-runtime-execstring-work-for-some-but-not-all-commands and https://stackoverflow.com/questions/48425729/whitespace-in-bash-path-with-java and probably more I can't find quickly – dave_thompson_085 May 29 '18 at 22:31

1 Answers1

5

try passing an array as suggested on javadocs.

String[] command = {"adb", "shell", "su -c 'settings put global airplane_mode_on 1'"};
LMC
  • 10,453
  • 2
  • 27
  • 52