public class MyProcess {
public static void main(String[] args) {
try {
// bash, su or any other command invoke a shell interpreter
// we execute the commands sent from outputstream by this
// interpreter
Process process = Runtime.getRuntime().exec("bash\n");
DataOutputStream outputStream = new DataOutputStream(
process.getOutputStream());
outputStream.writeBytes("echo nothing\n");
outputStream.writeBytes("ls -al\n");
outputStream.writeBytes("exit\n");
outputStream.flush();
outputStream.close();
process.waitFor();
BufferedReader rd = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String string = null;
while ((string = rd.readLine()) != null) {
System.out.println(string);
}
} catch (Exception e) {
// TODO: handle exception
}
}
Execute multiple commands by outputstream in Java
最新推荐文章于 2017-04-20 16:07:29 发布