加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Java调用cmd命令并获取执行结果

(2016-11-22 10:32:24)
标签:

java调用cmd命令行

分类: java
  1. import java.io.BufferedReader;  
  2. import java.io.InputStreamReader;  
  3.   
  4. public class Command  
  5.     public static void exeCmd(String commandStr)  
  6.         BufferedReader br null 
  7.         try  
  8.             Process Runtime.getRuntime().exec(commandStr);  
  9.             br new BufferedReader(new InputStreamReader(p.getInputStream()));  
  10.             String line null 
  11.             StringBuilder sb new StringBuilder();  
  12.             while ((line br.readLine()) != null 
  13.                 sb.append(line "\n");  
  14.              
  15.             System.out.println(sb.toString());  
  16.         catch (Exception e)  
  17.             e.printStackTrace();  
  18.           
  19.         finally  
  20.          
  21.             if (br != null 
  22.              
  23.                 try  
  24.                     br.close();  
  25.                 catch (Exception e)  
  26.                     e.printStackTrace();  
  27.                  
  28.              
  29.          
  30.      
  31.   
  32.     public static void main(String[] args)  
  33.         String commandStr "ping www.taobao.com" 
  34.         //String commandStr "ipconfig";  
  35.         Command.exeCmd(commandStr);  
  36.      
  37. }  

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有