代码很简单,例子如下:
String[] command = {
"/bin/sh",
"-c",
"whoami"
};
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(command);
process.waitFor();
StringBuffer sb = new StringBuffer();
BufferedReader reader= new BufferedReader(new InputStreamReader(process.getInputStream()));
String message=new String("");
while((message=reader.readLine())!=null){
sb.append(message);
}
System.out.println(sb.toString());