Java ProcessBuilder 找不到文件?!

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9847242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:32:17  来源:igfitidea点击:

ProcessBuilder can't find file?!

javabatch-fileprocessbuilder

提问by Rookie

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

回答by Ash

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

我正在运行 Linux,但在运行您的代码时发生了同样的错误(修改为运行 .sh 而不是 .bat)。

Try:

尝试:

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

Apparently using ProcessBuilder.directorydoesn'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?)

显然使用ProcessBuilder.directory不会影响构建构建器时选择的工作目录(为了发现可执行文件)(至少,这似乎是发生的事情。文档说它会改变工作目录,所以我猜输入/output 文件可能与此相关?)

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 ProcessBuilderdiscover an executable.

这篇文章讨论了这个问题和这个解决方案,但也提出了是否必须设置环境变量,其中“路径”类变量可能有助于ProcessBuilder发现可执行文件。

回答by 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:

嗨,尝试使用这里的教程 - http://www.javabeat.net/examples/2007/08/21/using-the-new-process-builder-class/。使用它我稍微改变了你的班级,它找到了文件(请注意,我不知道里面有什么,所以不能完全测试它)。它编译和运行没有问题,而你自己的我遇到和你一样的问题:

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

 }


}