Question

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
Was it helpful?

Solution

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top