我正在尝试从java程序运行一些Mercurial命令。我使用这样的Process
构建我的ProcessBuilder
:
final ProcessBuilder procBuilder = new ProcessBuilder("hg", "log");
procBuilder.directory(new File("/Users/feuerball/workspace/www"));
final Process proc = procBuilder.start();
文件夹www
包含Mercurial存储库,hg
已安装在系统PATH
中。但是当我启动这个过程时,我的程序抛出异常。这是堆栈跟踪:
Exception in thread "main" java.io.IOException: Cannot run program "hg" (in directory "/Users/feuerball/workspace/www"): error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1042)
at de.feuerball.tests.Test.main(Test.java:16)
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:185)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1023)
... 1 more
为什么我会收到此错误?
更新
为了表明目录确实存在,我稍微更改了代码:
final File repo = new File("/Users/feuerball/workspace/www");
System.out.println("Directory? " + repo.isDirectory());
System.out.println("Readable? " + repo.canRead());
System.out.println("Writable? " + repo.canWrite());
final ProcessBuilder procBuilder = new ProcessBuilder("hg", "log");
procBuilder.directory(repo);
final Process proc = procBuilder.start();
现在这是证据:
Directory? true
Readable? true
Writable? true
Exception in thread "main" java.io.IOException: Cannot run program "hg" (in directory "/Users/feuerball/workspace/www"): error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1042)
at de.brushmate.tests.Test.main(Test.java:22)
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:185)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1023)
... 1 more
答案 0 :(得分:3)
ProcessBuilder
没有使用env变量PATH,它无法找到&#34; hg&#34;,您需要指定&#34; hg&#34的绝对路径; (比如/ usr / bin / hg如果你正在使用Linux)