[英]java.io.IOException: Cannot run program “C:\AutoIt\ModenaAutoIt.exe”: java.io.IOException: error=2, No such file or directory
While doing the automation of a web application using selenium webdriver, I came across a situation where i need to Upload a file and proceed further. 在使用selenium webdriver自动化Web应用程序时,遇到了需要上传文件并继续进行操作的情况。
We are using Java & Tcl scripting language for this. 我们为此使用Java和Tcl脚本语言。
Below is my TCL code: 以下是我的TCL代码:
set methodname "uploadFile"
set action "Open"
set file "C:\\\\BATFiles\\\\InsertUsersAccessGroup.txt"
[$_webdriverObj executeScript $methodname $action $file] --> This calls the java method 'executeScript'
Here 'executeScript' is my Java method, coded as below: 这里的“ 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);
}
}
Even though the file "ModenaAutoIt.exe" is present in the 'C' directory under 'AutoIt' folder, my script is failing with the Java exception 即使“ AutoIt”文件夹下的“ C”目录中存在文件“ ModenaAutoIt.exe”,我的脚本也因Java异常而失败
java.io.IOException: Cannot run program "C:\\AutoIt\\ModenaAutoIt.exe": java.io.IOException: error=2, No such file or directory"
java.io.IOException:无法运行程序“ C:\\ AutoIt \\ ModenaAutoIt.exe”:java.io.IOException:error = 2,没有此类文件或目录”
Can someone please help me here? 有人可以在这里帮我吗?
This code works fine here, maybe you check with our example call. 这段代码在这里工作正常,也许您可以通过示例调用进行检查。 It also includes the output of the called executable:
它还包括调用的可执行文件的输出:
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();
}
}
I was getting the same exception today. 我今天也遇到同样的例外。
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)
The reason was i was not passing the exact location of the .exe file to the Runtime.getRuntime().exec(command) method. 原因是我没有将.exe文件的确切位置传递给Runtime.getRuntime()。exec(command)方法。
Instead of sending below address-->> 而不是发送以下地址->>
String command ="\"C:/Users/Casper/Desktop/Resource/Down.exe\"";
I was sending--> 我正在发送->
String command ="\"C:/Users/Casper/Desktop/Down.exe\"";
and because of that was getting the exception. 因此,这就是例外。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.