兼容timeout的Java执行外部命令的小程序

本文介绍了一个简单的Java程序,用于执行外部命令并具备超时控制功能。该程序能够较好地兼容不同的操作系统,并通过循环检查来实现对外部进程的超时管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转贴请注明出处: http://blog.youkuaiyun.com/froole

一个简单的兼容timeout的Java执行外部命令的小程序。
单线程,易于测试。
应该可以满足大多数后台程序的需要。

要注意的事,超过timeout时间之后,根据系统的不同,有可能无法马上强制停止process,这个时候只能用waitFor判断程序是否结束。
  1. /**
  2.  * 让Java执行外部命令兼容timeout<br>
  3.  * timeout誤差<=0.5秒
  4.  *
  5.  * @version 1.0
  6.  * @author 郝春利
  7.  */
  8. public class CommandExec {
  9.     /**
  10.      * 计算间隔时间
  11.      */
  12.     private static final int TIME_MARGIN = 500;
  13.     /**
  14.      * 命令
  15.      */
  16.     private String command;
  17.     /**
  18.      * timeout。単位:milli秒
  19.      */
  20.     private int timeout;
  21.     /**
  22.      *
  23.      *
  24.      * @param command
  25.      * @param timeout
  26.      */
  27.     public CommandExec(String command, Integer timeout) {
  28.         this.command = command;
  29.         this.timeout = timeout;
  30.     }
  31.     /**
  32.      *
  33.      * 执行命令<br>
  34.      *
  35.      * @param options
  36.      *            命令的参数
  37.      * @param dir
  38.      *            作业目录
  39.      * @return 返回执行结果的状态。
  40.      * @throws IOException
  41.      *             I/O异常
  42.      */
  43.     public Process exec(String[] options, File dir) throws IOException {
  44.         String[] commands = null;
  45.         if (options == null) {
  46.             commands = new String[] { command };
  47.         } else {
  48.             commands = new String[options.length + 1];
  49.             commands[0] = command;
  50.             for (int i = 0; i < options.length; i++) {
  51.                 commands[i + 1] = options[i];
  52.             }
  53.         }
  54.         Process process = Runtime.getRuntime().exec(commands, null, dir);
  55.         long limitTime = timeout + System.currentTimeMillis();
  56.         Integer status = null;
  57.         do {
  58.             try {
  59.                 status = process.exitValue();
  60.                 break;
  61.             } catch (IllegalThreadStateException e) {
  62.                 try {
  63.                     Thread.sleep(TIME_MARGIN);
  64.                 } catch (InterruptedException we) {
  65.                     throw new RuntimeException("err.fail.wait");
  66.                 }
  67.             }
  68.         } while (System.currentTimeMillis() < limitTime);
  69.         if (status == null) {
  70.             
  71.             process.destroy();
  72.             try {
  73.                 status = process.exitValue();
  74.             } catch (IllegalThreadStateException e) {
  75.                 
  76.             }
  77.         }
  78.         return process;
  79.     }
  80.     public ByteArrayOutputStream getOutputStream(InputStream input) throws IOException {
  81.         ByteArrayOutputStream out = new ByteArrayOutputStream();
  82.         for (int ch = 0; (ch = input.read()) != -1;) {
  83.             out.write(ch);
  84.         }
  85.         return out;
  86.     }
  87. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值