'Android java process returns 1 when executing "su"

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



Solution 1:[1]

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

So there's something not working right with your root

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 georgiecasey
Solution 2 Dinesh Prajapati