JVM执行WINDOWS命令的方法

  1. public class ExecCommand {
  2.     public ExecCommand() {
  3.     }
  4.     /**
  5.      * 执行一条命令
  6.      * 
  7.      * @param execStr
  8.      *            String 命令字符串
  9.      * @return String 执行命令错误时的信息。
  10.      */
  11.     public void exec(String execStr) {
  12.         System.out.println("exec this function");
  13.         Runtime runtime = Runtime.getRuntime(); // 取得当前运行期对象
  14.         String outInfo = ""// 执行错误的输出信息
  15. //      String[] args = new String[] { "sh", "-c", execStr };// 执行linux下的命令
  16. //       执行windows下的命令
  17.         String[] args = new String[] {"cmd""-c", execStr};
  18.         Process proc = null;
  19.         try {
  20.             proc = runtime.exec(args);
  21.             System.out.println("exec this function end");
  22.             InputStream in = proc.getErrorStream();// 得到错误信息输出。
  23.             
  24.             if(in==null){
  25.                 System.out.println("The executive result is null");
  26.             }
  27.             
  28.             BufferedReader br = new BufferedReader(new InputStreamReader(in));
  29.             String line = "";
  30.             while ((line = br.readLine()) != null) {
  31.                 outInfo = outInfo + line + "/n";
  32.                 System.out.println("The executive result is null");
  33.                 System.out.println(outInfo);
  34.             }
  35.         } catch (IOException e1) {
  36.             // TODO Auto-generated catch block
  37.             e1.printStackTrace();
  38.         } // 启动另一个进程来执行命令
  39.         // 检查命令是否失败。
  40.         try {
  41.             
  42.             if (proc.waitFor() != 0) {
  43.                 System.err.println("exit value = " + proc.exitValue());
  44.                 System.out.println("exec this function end");
  45.             }
  46.         } catch (InterruptedException e) {
  47.             System.err.print(e);
  48.             e.printStackTrace();
  49.         }
  50.     
  51.     }
  52.     /*
  53.     * ping the server
  54.     * @param server String
  55.     * @param timeout int
  56.     * @return boolean
  57.     * @throws IOException
  58.     */
  59.    public  boolean pingServer(String server,int timeout)
  60.    {
  61.        BufferedReader in = null;
  62.        Runtime r = Runtime.getRuntime(); 
  63.        String pingCommand = "ping " + server + " -w " + timeout;
  64.        try
  65.        {
  66.            Process p = r.exec(pingCommand);
  67.            if (p == null)
  68.            {
  69.                return false;
  70.            }
  71.            in = new BufferedReader(new InputStreamReader(p.
  72.                getInputStream()));
  73.            String line = null;
  74.            while ( (line = in.readLine()) != null)
  75.            {
  76.                System.out.println(line);
  77.                if (line.startsWith("Reply from"))
  78.                {
  79.                    return true;
  80.                }
  81.            }
  82.            in.close();
  83.        }
  84.        catch (Exception ex)
  85.        {
  86.            return false;
  87.        }
  88.        return false;
  89.    }
  90.     
  91.     
  92. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值