java.io.IOException:无法运行程序“C:\ AutoIt \ ModenaAutoIt.exe”:java.io.IOException:error = 2,没有这样的文件或目录

时间:2014-02-18 11:34:17

标签: java selenium-webdriver

在使用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);

    }

}

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

  

java.io.IOException:无法运行程序“C:\ AutoIt \ ModenaAutoIt.exe”:java.io.IOException:error = 2,没有这样的文件或目录“

有人可以帮我吗?

2 个答案:

答案 0 :(得分: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();
    }
}

答案 1 :(得分:1)

我今天得到了同样的例外。

 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\"";

因为这是例外。