【问题标题】:java.io.IOException: Cannot run program "C:\AutoIt\ModenaAutoIt.exe": java.io.IOException: error=2, No such file or directoryjava.io.IOException:无法运行程序“C:\AutoIt\ModenaAutoIt.exe”:java.io.IOException:错误=2,没有这样的文件或目录
【发布时间】:2014-02-18 11:34:16
【问题描述】:

在使用 selenium webdriver 自动化 Web 应用程序时,我遇到了需要上传文件并继续进行的情况。

我们为此使用 Java 和 Tcl 脚本语言。

下面是我的 TCL 代码:

set methodname "uploadFile"

set action "Open"

set file "C:\\\\BATFiles\\\\InsertUsersAccessGroup.txt"

[$_webdriverObj executeScript $methodname $action $file] --> This calls the java method 'executeScript'

这里的 'executeScript' 是我的 Java 方法,代码如下:

public void executeScript(String methodName, String action,String file) {

    log.info("Before try block");
    try {
        log.info("Inside try block");
        Runtime r = Runtime.getRuntime();
        log.info("Created a runtime object");
        Process p = r.exec(new String[]{"C:\\AutoIt\\ModenaAutoIt.exe", methodName, action, file });
        log.info("Afte the exec");
        p.waitFor();

    } catch(Exception IOException) {
        log.info("inside exception");
        log.info(IOException);

    }

}

即使文件“ModenaAutoIt.exe”存在于“AutoIt”文件夹下的“C”目录中,我的脚本仍因 Java 异常而失败

java.io.IOException: 无法运行程序 "C:\AutoIt\ModenaAutoIt.exe": java.io.IOException: error=2, No such file or directory"

有人可以帮我吗?

【问题讨论】:

  • 可能是权限问题?你有没有尝试在那个盒子上直接运行你的代码(简单的 main 没有 web-stuff)?
  • 测试如下: public static void main(String args[]){ WebHelper wh = new WebHelper(); wh.executeScript("uploadFile", "Open", "C:\\\\BATFiles\\\\InsertUsersAccessGroup.txt"); }
  • 它给出了这样的输出,即使它实际上没有执行上传功能: (17:16:18,010) WebHelper : INFO - Before try block (17:16:18,013) WebHelper :INFO - 内部尝试块(17:16:18,013)WebHelper:INFO - 创建了一个运行时对象(17:16:18,105)WebHelper:INFO - 执行后
  • 你怎么知道它是否运行?您没有捕获进程的 system.out/system.err 输出。此外,在 waitFor() 调用之前,您有“执行之后”日志,这有点不现实。

标签: java selenium-webdriver


【解决方案1】:

此代码在这里运行良好,也许您可​​以查看我们的示例调用。它还包括被调用可执行文件的输出:

package test;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ProcBuilderTest {
    public static void main(String[] args) throws Exception {
        final ProcessBuilder pb = new ProcessBuilder("C:/WINDOWS/system32/notepad.exe", "d:/tmp/tmp.txt");
        pb.redirectErrorStream(true);
        final Process p = pb.start();
        BufferedReader res = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String commandOutput = "";
        String tmp;
        while ((tmp = res.readLine()) != null) {
            commandOutput += tmp;
        }
        System.out.println("output:" + commandOutput);
        if (p.waitFor() != 0) {
            System.out.println("exit value is: " + p.exitValue());
            return;
        }
        p.destroy();
    }
}

【讨论】:

    【解决方案2】:

    我今天也遇到了同样的异常。

     Exception- 
            java.io.IOException: Cannot run program ""C:/Users/Casper/Desktop/Down.exe"null": CreateProcess error=2, The system cannot find the file specified
            at java.lang.ProcessBuilder.start(Unknown Source)
            at java.lang.Runtime.exec(Unknown Source)
            at java.lang.Runtime.exec(Unknown Source)
            at java.lang.Runtime.exec(Unknown Source) 
    

    原因是我没有将 .exe 文件的确切位置传递给 Runtime.getRuntime().exec(command) 方法。

    而不是发送以下地址-->>

          String command ="\"C:/Users/Casper/Desktop/Resource/Down.exe\""; 
    

    我正在发送-->

      String command ="\"C:/Users/Casper/Desktop/Down.exe\"";
    

    因此出现异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-22
      • 2019-03-17
      • 1970-01-01
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多