ProcessBuilder can't find file?!

别说谁变了你拦得住时间么 提交于 2019-11-29 10:08:28

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.

aretai

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();

 }


}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!