ProcessBuilder cannot find the file ?!

Another question in quick succession, but it should be a really obvious mistake that I do not see. I wrote the code to run the batch file below, but I get an error message saying that he cannot find the file, but I can assure that the file exists 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 
+8
java batch-file processbuilder
source share
2 answers

I run Linux, but the same error occurs when I run your code (modified to run .sh, not .bat).

Try:

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

Apparently, using ProcessBuilder.directory does not affect the working directory (to detect the executable) that was selected when the builder was built (at least this is what seems to be happening). The docs say this will change working, so I think I / O files can relate to this?)

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

This post talks about the problem and this solution , but also raises whether environment variables should be set from which the β€œpath” variables can be useful to help ProcessBuilder detect the executable.

+11
source share

Hi, try using the tutorial here - http://www.javabeat.net/examples/2007/08/21/using-the-new-process-builder-class/ . Using it, I changed my class a bit and found the file (note that I do not know what is inside, so it cannot fully test it). It compiles and runs without problems, while I have the same problems as yours:

 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(); } } 
+2
source share

All Articles