Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set working directory with ProcessBuilder

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 
like image 798
Eric Avatar asked Dec 06 '11 19:12

Eric


People also ask

How does ProcessBuilder work in Java?

Constructs a process builder with the specified operating system program and arguments. This is a convenience constructor that sets the process builder's command to a string list containing the same strings as the command array, in the same order.

What is ProcessBuilder?

A Process Builder is a tool provided by Salesforce to help you automate your process by updating or creating a record.


1 Answers

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(); 
like image 83
dmeister Avatar answered Oct 11 '22 01:10

dmeister