12

Another question in quick succession but this has to be a really obvious error that I am not seeing. I've written some code to run a batch file below but I'm getting an error message saying it cannot find the file but I can assure you that the file does exist in the directory!

public class Pull {

public void pullData() throws IOException {
    ProcessBuilder pb = new ProcessBuilder("adb.bat");
    File f = new File("C:\\");
    pb.directory(f);
    Process p = pb.start();  
}

 public static void main(String[] args) throws IOException {
     Pull pull = new Pull();
     pull.pullData();
 }

}

and here is the error message

Exception in thread "main" java.io.IOException: Cannot run program "adb.bat" (in directory "C:\"): CreateProcess error=2, The system cannot find the file specified
Rookie
  • 1,879
  • 3
  • 17
  • 18
  • So, adb.bat is in c:\, right? try placing it or a copy of it in the project folder – keyser Mar 23 '12 at 22:37
  • doesn't work there either. Am I using processbuilder correctly? – Rookie Mar 23 '12 at 22:47
  • Have you tried executing the contents of the batch file as the arguments to your `ProcessBuilder`? E.g., if "adb.bat" does something like "java -version", have you tried doing something like `new ProcessBuilder("java","-server")`? I'm wondering if Java is trying to treat adb.bat like an executable, even though it's really just a text-based script. – CodeBlind Mar 23 '12 at 22:58
  • Have you tried including the path in the constructor (eg, `new ProcessBuilder("c:\\adb.bat")`? See http://www.coderanch.com/t/517098/java/java/Working-ProcessBuilder-working-directory – Ash Mar 23 '12 at 23:00

2 Answers2

13

I'm running Linux, but the same error occurs when I run your code (modified to run a .sh rather than .bat).

Try:

ProcessBuilder pb = new ProcessBuilder("c:\\adb.bat");

Apparently using ProcessBuilder.directory doesn't affect the working directory (for the purposes of discovering the executable) that was chosen when the builder was constructed (at least, that's what seems to happen. The docs say it will change the working directory, so I guess input/output files might be relative to that?)

I'm not sure what it's actually doing internally, but providing the path to the executable in the constructor fixed the problem.

This post talks about the problem and this solution, but also raises whether environment variables have to be set, of which "path"-like variables might be useful to help ProcessBuilder discover an executable.

Ash
  • 9,296
  • 2
  • 24
  • 32
  • 2
    On Linux, if a `command` is in `directory`, and using the `ProcessBuilder` to change to that directory, when just executing `command` will fail, but executing `./command` tends to work. So the process builder is changing into the directory, but one needs to specifically execute `./command` in that directory, unless the directory is on the `$PATH`. – Kay Jun 05 '18 at 20:14
2

Hi try to use the tutorial here - http://www.javabeat.net/examples/2007/08/21/using-the-new-process-builder-class/. Using it I have changed your class a bit and it finds the file (note that I don't know what is inside so can't fully test it). It compiles and runs without problem, while your own I experience same problems as you:

public class Pull {


public void pullData() throws IOException {
    /*ProcessBuilder pb = new ProcessBuilder("adb.bat");
    File f = new File("C:\\");
    pb.directory(f);
    Process p = pb.start(); 
    */
    ProcessBuilder p = new ProcessBuilder("C:\\adb.bat");
     p.start();
    System.out.println(p.toString());
}


 public static void main(String[] args) throws IOException {


     Pull pull = new Pull();
     pull.pullData();

 }


}
thSoft
  • 21,755
  • 5
  • 88
  • 103
aretai
  • 1,619
  • 6
  • 19
  • 30