public class Shellcommand
{
public static void ExecCmd (String cmd, boolean wait)
{
ExecCmd (cmd, wait, null);
}
public static void ExecCmd (String cmd, boolean wait, StringBuilder output)
{
string[] Cmds = new string[] {cmd};
EXECCMDS (CMDS, NULL, NULL, wait, output);
}
public static void Execcmds (string[] cmd, String dir, boolean wait,
StringBuilder output)
{
Execcmds (cmd, null, dir, wait, output);
}
public static void Execcmds (string[] cmds, string[] env, String dir,
Boolean wait, StringBuilder output)
{
Process process = NULL;
try {
File dirfile = null;
if (dir! = null) {
Dirfile = new File (dir);
}
for (String Cmd:cmds) {
System.out.println (CMD);
}
if (cmds.length = = 1) {
Process = Runtime.getruntime (). EXEC (Cmds[0], env, dirfile);
} else {
Process = Runtime.getruntime (). EXEC (Cmds, env, dirfile);
}
Logprocessoutput (process, cmds[0], output);
} catch (Exception e) {
String message = "Executecmd:" + Cmds + "error:" + e.tostring ();
if (output! = null) {
Output.append (message);
}
}
if (process! = null) {
if (wait) {
try {
Process.waitfor ();
Process.getoutputstream (). Close ();
Process.geterrorstream (). Close ();
Process.getinputstream (). Close ();
} catch (Interruptedexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}
public static Process runprocess (string[] Cmds, Boolean root, string[] env,
String dir, boolean wait, StringBuilder output)
{
Process process = NULL;
try {
File dirfile = null;
if (dir! = null) {
Dirfile = new File (dir);
}
for (String Cmd:cmds) {
System.out.println (CMD);
}
if (root) {
Process = Runtime.getruntime (). EXEC ("su");
DataOutputStream OS = new DataOutputStream (
Process.getoutputstream ());
for (String Cmd:cmds) {
Os.writebytes (cmd + "\ n");
}
Os.flush ();
Os.writebytes ("exit\n");
Os.close ();
} else {
if (cmds.length = = 1) {
Process = Runtime.getruntime (). EXEC (Cmds[0], env, dirfile);
} else {
Process = Runtime.getruntime (). EXEC (Cmds, env, dirfile);
}
}
} catch (Exception e) {
String message = "Executecmd:" + Cmds + "error:" + e.tostring ();
if (output! = null) {
Output.append (message);
}
}
if (process! = null) {
if (wait) {
try {
Process.waitfor ();
Process.getoutputstream (). Close ();
Process.geterrorstream (). Close ();
Process.getinputstream (). Close ();
} catch (Interruptedexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
return process;
}
}
Java Code call EXE (CMD command)