asfsd 发表于 2018-6-22 17:32:32

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();
  

  
}
页: [1]
查看完整版本: Java执行windows的cmd命令