的ProcessBuilder找不到文件? [英] ProcessBuilder can't find file?!

查看:2197
本文介绍了的ProcessBuilder找不到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在连续快速的另一个问题,但这显然是因为我没有看到一个真正明显的错误。我已经写了一些code运行下面的批处理文件,但我得到一个错误信息说无法找到该文件,但我可以向你保证文件不存在目录中!

 公共类拉{公共无效pullData()抛出IOException
    PB的ProcessBuilder =新的ProcessBuilder(adb.bat);
    文件f =新的文件(C:\\\\);
    pb.directory(F);
    进程p = pb.start();
} 公共静态无效的主要(字串[] args)抛出IOException
     拽拽=新拉();
     pull.pullData();
 }

}

和这里的错误信息

 异常线程mainjava.io.IOException异常:不能运行程序adb.bat(在目录C:\\):CreateProcess的错误= 2,系统不能找到指定的文件


解决方案

我在运行Linux,但是当我运行code(修改运行.SH而不是.bat)的形式出现同样的错误。

尝试:

 的ProcessBuilder PB =新的ProcessBuilder(C:\\\\ adb.bat);

显然使用 ProcessBuilder.directory 不影响工作目录(用于发现可执行文件的目的的)被选择时,建设者构建了(至少,那是什么似乎发生。该文档说,这将改变工作目录,所以我想输入/输出文件可能是相对的?)

我不知道它实际上做内部的东西,而是要提供在构造函数中的可执行文件路径解决了这一问题。

这后会谈>,但也引发了是否环境变量已经被设置,其中路径般的变量可能是有用的帮助的ProcessBuilder 发现一个可执行文件。

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

解决方案

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.

这篇关于的ProcessBuilder找不到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆