37

I am trying start a process in my home directory in ubuntu. I keep getting a permission denied exception and I have no idea why. Here is the code:

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

Here is the exception:

Exception in thread "main" java.io.IOException: Cannot run program "/home": 

java.io.IOException: error=13, Permission denied
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:475)
        at tester.Main.main(Main.java:30)
Caused by: java.io.IOException: java.io.IOException: error=13, Permission denied
        at java.lang.UNIXProcess.<init>(UNIXProcess.java:164)
        at java.lang.ProcessImpl.start(ProcessImpl.java:81)
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:468)
        ... 1 more
Java Result: 1
Makyen
  • 31,849
  • 12
  • 86
  • 121
Eric
  • 718
  • 1
  • 9
  • 22

1 Answers1

86

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();
Matteo Mazza
  • 75
  • 1
  • 8
dmeister
  • 34,704
  • 19
  • 73
  • 95
  • Ok, thanks. I just edited the question because I realized the mistake I made. I think you may have answered my other question as well – Eric Dec 06 '11 at 19:49
  • 1
    I had to use ProcessBuilder pb = new ProcessBuilder(new String[]{"sh","do_foo.sh"}); in order to make it work. – Volokh Jan 16 '20 at 16:47
  • I had to use "sh -c ...": Process process = new ProcessBuilder(['sh', '-c', "zip -r /tmp/$orderId /tmp/$orderId/".toString()]) (this is Groovy syntax) – Jose Quijada Aug 19 '22 at 16:38