1

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.

We are using Java & Tcl scripting language for this.

Below is my TCL code:

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:

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

java.io.IOException: Cannot run program "C:\AutoIt\ModenaAutoIt.exe": java.io.IOException: error=2, No such file or directory"

Can someone please help me here?

4
  • Maybe a permission problem? Did you try to run your code directly (simple main without web-stuff) on that box? Commented Feb 18, 2014 at 11:38
  • Tested as below: public static void main(String args[]){ WebHelper wh = new WebHelper(); wh.executeScript("uploadFile", "Open", "C:\\\\BATFiles\\\\InsertUsersAccessGroup.txt"); } Commented Feb 18, 2014 at 11:48
  • It has given the output like this, even though it actually didn't perform the upload function: (17:16:18,010) WebHelper : INFO - Before try block (17:16:18,013) WebHelper : INFO - Inside try block (17:16:18,013) WebHelper : INFO - Created a runtime object (17:16:18,105) WebHelper : INFO - Afte the exec Commented Feb 18, 2014 at 11:49
  • How would you know if it ran or not? You don't capture the system.out/system.err output of the process. Besides you have your "after the exec" log BEFORE the waitFor() call which is kind of not realistic. Commented Feb 18, 2014 at 12:47

2 Answers 2

1

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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.