2

i'm trying to write onto the "/data/myFolder" folder on the android virtual device during test and to do that, i do:

String[] cmd ={"su", "mkdir", dir};
    int out = 99;

    try {

        Process p = Runtime.getRuntime().exec(cmd);
        out = p.waitFor();
        BufferedInputStream in = new BufferedInputStream(p.getErrorStream());
        byte[] bytes = new byte[4096];
        while (in.read(bytes) != -1) {

        }

        in.close();
        logger.info("exit status:" + out);

    } catch (IOException e) {
        logger.severe("IOException " + e.getMessage());
        e.printStackTrace();
    } catch (InterruptedException e) {
        logger.severe("InterruptedException " + e.getMessage());
        e.printStackTrace();
    }
    if (out != 0) {
        logger.severe("Folder " + dir + " not created,exit status: " + out);
    }

or i've tried

String cmd ={"su mkdir" + dir}; 

but the exit status is 1 and it not create any folder. using su mkdir /data/myFolder from adb shell works fine. Why this code? what means? (i know it means that something went wrong, but what? didn't find any documentation about android mkdir exit code values). Thank's

Torben
  • 3,805
  • 26
  • 31
idell
  • 107
  • 1
  • 2
  • 13
  • What happens if you add a space to the thing you tried: `String cmd = "su mkdir " + dir;`? – Jite Nov 13 '13 at 11:50
  • possible duplicate of [what does the su mean: process = Runtime.getRuntime().exec("su");](http://stackoverflow.com/questions/6100662/what-does-the-su-mean-process-runtime-getruntime-execsu) – Torben Nov 13 '13 at 12:18
  • i get the same error code (1) – idell Nov 13 '13 at 12:19
  • 1
    Of course you get. You are still executing "su", not "mkdir". Please do a Google search about what the "su"-command actually does. – Torben Nov 13 '13 at 12:30
  • @Torben you are right, i was executing su command and not mkdir as root. now String cmd = "mkdir "+ dir; Process p = Runtime.getRuntime().exec(cmd); works fine. – idell Nov 13 '13 at 12:41

2 Answers2

2

error code 1 is EPERM /* Not super-user */ on Android if anyone is curious.

So there's something not working right with your root

georgiecasey
  • 21,793
  • 11
  • 65
  • 74
0

I use below code to give commands

try {
            Process sh = Runtime.getRuntime().exec("su", null, null);
            OutputStream os = sh.getOutputStream();
            byte[] mScreenBuffer = ("mkdir dir ")
                    .getBytes();
            os.write(mScreenBuffer);
            os.flush();
            os.close();
            sh.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }
Dinesh Prajapati
  • 9,274
  • 5
  • 30
  • 47