匿名用户
1级
2014-02-13 回答
执行linux命令基,基本思路是从控制台获得输入的指令,启动命令行执行命令,捕捉异常,示例如下:
public class TestRunTime {
public static void main(String[] args) throws IOException, InterruptedException {
String cmd = "";
if(args == null || args.length == 0){
System.out.println("请输入命令行参数");
}else{
for(int i=0;i
cmd += args[i] + " ";
}
}
try {
Process process = Runtime.getRuntime().exec(cmd);//执行命令
InputStreamReader ir = new InputStreamReader(process.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
while ((line = input.readLine()) != null) {//输出结果
System.out.println(line);
}
} catch (java.io.IOException e) {
System.err.println("IOException " + e.getMessage());//捕捉异常
}
}
}