Java執行windows的cmd命令

 Runtime runtime=Runtime.getRuntime();

  String command="cmd.exe /c ipconfig /all";

  Process process=null;

  try {

   System.out.println("start");

   process=null;

   process=runtime.exec(command);

   System.out.println("finish");

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

  if(process==null){

   return;

  }

  BufferedReader reader=null;

  try {

   reader = new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK"));

  } catch (UnsupportedEncodingException e1) {

   // TODO Auto-generated catch block

   e1.printStackTrace();

  }

  if(reader==null){

   return;

  }

  try {

   String s=reader.readLine();

   while(s!=null){

    System.out.println(s);

    s=reader.readLine();

   }

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章