关于java调用Autoit脚本的问题

本文介绍如何在Java中调用AutoIt脚本并实现超时控制,通过检查进程存在与否及设置超时时间确保程序稳定运行。

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

原文地址:http://www.zhenghongzhi.cn/post/69.html

由于selenium本身的局限性,我们在做web测试的时候,不得不使用autoit作为辅助工具来处理一些windows窗口的操作。

先来段autoit代码:

  1. Sleep($CmdLine[1]) 
编译成exe文件,文件名为sleep.exe,放在c盘根目录下,代码的功能很简单,就是根据传入的时间休眠。cmd下执行:c:\sleep.exe 2000,表示休眠2秒。 

我们在Java中调用这个程序:
  1. long time1 = System.currentTimeMillis();
  2.                 try {
  3.                         Runtime.getRuntime().exec("c://sleep.exe 10000");
  4.                         long time2 = System.currentTimeMillis();
  5.                         System.out.println((time2 - time1) + "毫秒。");
  6.                 } catch (Exception e) {
  7.                         e.printStackTrace();
  8.                 }
用上面这段代码去调用的话,java不会等待autoit的执行,sleep启动后,继续往下执行,这样几乎没有实际的应用价值。我们通常都是要等autoit去把该做的事情都做完再做后面的事情。
  1. long time1 = System.currentTimeMillis();
  2.                 try {
  3.                         Runtime.getRuntime().exec("c://sleep.exe 10000").waitFor();
  4.                         long time2 = System.currentTimeMillis();
  5.                         System.out.println((time2 - time1) + "毫秒。");
  6.                 } catch (Exception e) {
  7.                         e.printStackTrace();
  8.                 }
我们在代码中加入waitFor(),可以达到上面的效果,但是又带来了另外一个问题,玩意autoit执行出了问题,一直不退出怎么办?程序就hang住了。

今天在某QQ群中向高人讨教了一番,知道了如何控制autoit的超时问题,我们可以利用检查内存中是否存在au3进程的方式来做判断程序是否执行完毕,同时加一个超时控制,超时则继续往后执行,下面是demo 代码:

Java代码:
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.InputStreamReader;
  5.  
  6. public class Runau3 {
  7.         public static void main(String[] args) {
  8.                 // 执行au3脚本
  9.                 // 休眠的那个脚本,脚本所在的目录(秒),等待时间,脚本的休眠时间(毫秒)
  10.                 runAu3("sleep.exe", "c://", 5, "10000");
  11.         }
  12.  
  13.         /**
  14.          * 
  15.          * @param filename
  16.          *            au3的文件名,必须编译成exe
  17.          * @param filepath
  18.          *            au3的文件的路径
  19.          * @param timeout
  20.          *            超时时间(秒)
  21.          * @param args
  22.          *            au3的参数
  23.          */
  24.         public static void runAu3(String filename, String filepath, int timeout,
  25.                         String... args) {
  26.                 try {
  27.                         // 如果发现进程,先杀了
  28.                         if (findProcess(filename)) {
  29.                                 killProcess(filename);
  30.                         }
  31.                         // 拼装命令
  32.                         String cmd = filename;
  33.                         for (String arg : args) {
  34.                                 cmd += " " + arg;
  35.                         }
  36.  
  37.                         // 执行au3脚本
  38.                         Runtime.getRuntime()
  39.                                         .exec("cmd /C " + cmd, null, new File(filepath));
  40.                         int count = 0;
  41.                         while (findProcess(filename) && count < timeout) {
  42.                                 System.out.println(count);
  43.                                 Thread.sleep(1000);
  44.                                 count++;
  45.                         }
  46.                         // 如果进程未结束,杀了
  47.                         if (findProcess(filename)) {
  48.                                 killProcess(filename);
  49.                         }
  50.                 } catch (Exception e) {
  51.                         e.printStackTrace();
  52.                 }
  53.  
  54.         }
  55.  
  56.         /**
  57.          * 查找进程
  58.          * @param processname
  59.          * @return
  60.          */
  61.         public static boolean findProcess(String processname) {
  62.                 BufferedReader bufferedreader = null;
  63.                 try {
  64.                         Process proc = Runtime.getRuntime().exec(
  65.                                         "tasklist /fi \" imagename eq " + processname + " \" ");
  66.                         bufferedreader = new BufferedReader(new InputStreamReader(proc
  67.                                         .getInputStream()));
  68.                         String line = null;
  69.                         while ((line = bufferedreader.readLine()) != null) {
  70.                                 if (line.contains(processname)) {
  71.                                         return true;
  72.                                 }
  73.                         }
  74.                         return false;
  75.                 } catch (Exception ex) {
  76.                         ex.printStackTrace();
  77.                         return false;
  78.                 } finally {
  79.                         if (bufferedreader != null) {
  80.                                 try {
  81.                                         bufferedreader.close();
  82.                                 } catch (Exception ex) {
  83.                                 }
  84.                         }
  85.                 }
  86.         }
  87.  
  88.         /**
  89.          * 杀进程
  90.          * @param processname
  91.          */
  92.         public static void killProcess(String processname) {
  93.                 BufferedReader bufferedreader = null;
  94.                 try {
  95.                         Process proc = Runtime.getRuntime().exec(
  96.                                         "taskkill /F /IM " + processname);
  97.                         bufferedreader = new BufferedReader(new InputStreamReader(proc
  98.                                         .getInputStream()));
  99.                         String line = null;
  100.                         while ((line = bufferedreader.readLine()) != null) {
  101.                                 System.out.println(line);
  102.                         }
  103.                 } catch (Exception ex) {
  104.                         ex.printStackTrace();
  105.                 } finally {
  106.                         if (bufferedreader != null) {
  107.                                 try {
  108.                                         bufferedreader.close();
  109.                                 } catch (Exception ex) {
  110.                                 }
  111.                         }
  112.                 }
  113.         }
  114. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值