Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

ProcessBuilder: set working directory not working?

807589Jul 24 2008 — edited Jul 25 2008
I'm trying to run an executable in a specified working directory using ProcessBuilder,
but the directory(File) method doesn't appear to work as the java docs suggest.
To simplify the problem, I wrote a piece of test code that exhibits the same behavior as my application.

I would expect this code to work:
import java.io.File;
import java.io.IOException;

public class test {
	public static void main(String[] args) throws IOException {
		ProcessBuilder pb = new ProcessBuilder("testme.bat");
		pb.directory(new File("c:\\testDir"));
		pb.start();
	}
}
But, it doesn't. I get this error:
Exception in thread "main" java.io.IOException: Cannot run program "testme.bat" (in directory "c:\testDir"):
  CreateProcess error=2, The system cannot find the file specified
	at java.lang.ProcessBuilder.start(Unknown Source)
	at test.main(test.java:8)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
	at java.lang.ProcessImpl.create(Native Method)
	at java.lang.ProcessImpl.<init>(Unknown Source)
	at java.lang.ProcessImpl.start(Unknown Source)
	... 2 more
And, yes, c:\testDir\testme.bat exists.
In fact, the above test code works if I run it from c:\testDir. It doesn't work if I run it from any other directory.

I thought the purpose of the directory(File) method was to set the working directory
of the ProcessBuilder before it executed the command. Am I misunderstanding the API description? What am I doing wrong?

Edited by: jack1323 on Jul 24, 2008 3:46 PM

Edited by: jack1323 on Jul 24, 2008 3:46 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 22 2008
Added on Jul 24 2008
3 comments
11,216 views