Java tutorial
//package com.java2s; //License from project: Open Source License import java.io.DataOutputStream; public class Main { private static Process sh = null; private static Process su = null; public static void ExecuteNoReturn(String command, Boolean useroot, boolean forcenew) throws Exception { Process p; DataOutputStream os; p = getProcess(useroot, forcenew); os = new DataOutputStream(p.getOutputStream()); os.writeBytes(command + "\n"); if (forcenew) { os.writeBytes("exit\n"); os.flush(); os.close(); } //p.waitFor(); } private static Process getProcess(boolean useroot, boolean forcenew) throws Exception { if (forcenew && useroot) return Runtime.getRuntime().exec(new String[] { "su" }); else if (forcenew && !useroot) return Runtime.getRuntime().exec(new String[] { "sh" }); else if (useroot && su != null) return su; else if (useroot && su == null) { su = Runtime.getRuntime().exec(new String[] { "su" }); return su; } else if (!useroot && sh != null) return sh; else if (!useroot && sh == null) { sh = Runtime.getRuntime().exec(new String[] { "sh" }); return sh; } /* Shouldn't ever actually get here! */ return Runtime.getRuntime().exec(new String[] { "sh" }); } }