Java调用Windows控制台命令

2014-11-12 11:30:10 · 作者: · 浏览: 32

  方法一:


  public static void main(String[] args)


  {


  InputStream ins = null;


  String[] cmd = new String[]{ "cmd.exe", "/C", "ipconfig" };


  try


  {


  Process process = Runtime.getRuntime().exec(cmd);


  // cmd 的信息


  ins = process.getInputStream();


  BufferedReader reader = new BufferedReader(new InputStreamReader(


  ins));


  String line = null;


  while ((line = reader.readLine()) != null)


  {


  // 输出


  System.out.println(line);


  }


  int exitValue = process.waitFor();


  System.out.println("返回值:" + exitValue);


  // 关闭


  process.getOutputStream().close();


  }


  catch (Exception e)


  {


  e.printStackTrace();


  }