我正在寻找一种从Java中执行多个shell命令的方法。我在stackoverflow上找到了这个方法,但它只能用于每个会话执行一个shell命令:
try {
// Execute command
String command = "ls -la";
StringBuffer ret=new StringBuffer();
Process p = Runtime.getRuntime().exec(command);
// Get the input stream and read from it
InputStream in = child.getInputStream();
int c;
while ((c = in.read()) != -1) {
ret.append((char)c);
}
in.close();
System.out.println(ret.toString());
} catch (IOException e) {
e.printStackTrace();
}
有没有一种方法使用上面的代码在同一个会话中执行多个命令?