简体   繁体   中英

Android error: write failed: EPIPE (Broken pipe)

I am trying to execute a command that needs root access in Android with the following:

public static void runCmd(String cmd) {
        DataOutputStream os;
        try {
            Process process = Runtime.getRuntime().exec("su");
            os = new DataOutputStream(process.getOutputStream());
            //os.writeBytes(cmd + "\n");
            os.writeBytes("echo hello\n");    <---- This fails?
            os.writeBytes("exit\n");
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

As you can see I've commented out the actual command to rule out that it has anything to do with root access ("echo hello" doesn't need root right?). I've used the code a lot on Android 4.2 devices, and now we have to use Android 4.4 and it doesn't work anymore. I get the error:

07-19 22:29:29.555 20744-20744/? W/System.err: java.io.IOException: write failed: EPIPE (Broken pipe)
07-19 22:29:29.555 20744-20744/? W/System.err:     at libcore.io.IoBridge.write(IoBridge.java:455)
07-19 22:29:29.555 20744-20744/? W/System.err:     at java.io.FileOutputStream.write(FileOutputStream.java:187)
07-19 22:29:29.555 20744-20744/? W/System.err:     at java.io.OutputStream.write(OutputStream.java:82)
07-19 22:29:29.555 20744-20744/? W/System.err:     at java.io.DataOutputStream.writeBytes(DataOutputStream.java:156)
07-19 22:29:29.555 20744-20744/? W/System.err:     at com.company.kioskapp.MainActivity.runCmd(MainActivity.java:298)
07-19 22:29:29.555 20744-20744/? W/System.err:     at com.benefittechnologies.benekiosk.MainActivity.disableAndroidUI(MainActivity.java:273)
07-19 22:29:29.555 20744-20744/? W/System.err:     at com.benefittechnologies.benekiosk.MainActivity.onCreate(MainActivity.java:124)
07-19 22:29:29.555 20744-20744/? W/System.err:     at android.app.Activity.performCreate(Activity.java:5254)
07-19 22:29:29.555 20744-20744/? W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-19 22:29:29.555 20744-20744/? W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
07-19 22:29:29.555 20744-20744/? W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
07-19 22:29:29.555 20744-20744/? W/System.err:     at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-19 22:29:29.555 20744-20744/? W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-19 22:29:29.555 20744-20744/? W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
07-19 22:29:29.555 20744-20744/? W/System.err:     at android.os.Looper.loop(Looper.java:136)
07-19 22:29:29.555 20744-20744/? W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5001)
07-19 22:29:29.555 20744-20744/? W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
07-19 22:29:29.555 20744-20744/? W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
07-19 22:29:29.555 20744-20744/? W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:801)
07-19 22:29:29.555 20744-20744/? W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:617)
07-19 22:29:29.555 20744-20744/? W/System.err:     at dalvik.system.NativeStart.main(Native Method)
07-19 22:29:29.555 20744-20744/? W/System.err: Caused by: libcore.io.ErrnoException: write failed: EPIPE (Broken pipe)
07-19 22:29:29.555 20744-20744/? W/System.err:     at libcore.io.Posix.writeBytes(Native Method)
07-19 22:29:29.555 20744-20744/? W/System.err:     at libcore.io.Posix.write(Posix.java:202)
07-19 22:29:29.555 20744-20744/? W/System.err:     at libcore.io.BlockGuardOs.write(BlockGuardOs.java:197)
07-19 22:29:29.555 20744-20744/? W/System.err:     at libcore.io.IoBridge.write(IoBridge.java:450)
07-19 22:29:29.555 20744-20744/? W/System.err:  ... 20 more 

If I comment out the

os.writeBytes("echo hello\\n");

line as well, no exceptions.

Turns out that is actually a root error after all. The device wasn't properly rooted. Check my other post about that: How to check if custom android is rooted?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM