java如何调用系统命令
java中可以通过Runtime来执行系统命令,大致操作如下:
Runtime rt = Runtime.getRuntime(); Process process = rt.exec("命令字符串"); //获取命令的输出 BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); String line = null; while ((line = in.readLine()) != null) { System.out.println(line); }
如果命令有错,把上述的process.getInputStream()替换成process.getErrorStream(),可以有效的获取操作系统给出的错误提示,帮我们分析命令出错的原因。
欢迎转载,转载请注明文章出处,谢谢!