How to set working directory with ProcessBuilder

ε祈祈猫儿з 提交于 2019-11-26 04:51:42

问题


I am trying start a process in my home directory in ubuntu. I am getting an array out of bounds exception. Here is the code:

Process p = null;
ProcessBuilder pb = new ProcessBuilder();
pb.directory(new File(\"/home\"));
p = pb.start();

Here is the exception:

Exception in thread \"main\" java.lang.ArrayIndexOutOfBoundsException: 0
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
    at tester.Main.main(Main.java:31)
Java Result: 1

回答1:


You are trying to execute /home and it is not an executable file. The constructor argument of the process builder is the command to execute.

You want to set the working directory. You can that it via the directory method.

Here is a complete example:

Process p = null;
ProcessBuilder pb = new ProcessBuilder("do_foo.sh");
pb.directory(new File("/home"));
p = pb.start();


来源:https://stackoverflow.com/questions/8405723/how-to-set-working-directory-with-processbuilder

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