Reputation: 3
For the second time I had a problem with values extracted from system calls using ProcessBuilder
.
@org.junit.Test
public void test() {
Process process = null;
ProcessBuilder pb = new ProcessBuilder("QQ.exe");
pb.directory(new File("D:\\Program Files (x86)\\Tencent\\QQ\\Bin\\"));
try {
process = pb.start();
} catch (IOException e) {
e.printStackTrace();
}
}
The result of above is: "Cannot run program "QQ.exe" (in directory "D:\Program Files (x86)\Tencent\QQ\Bin"): CreateProcess error=2, The system cannot find the file specified"
So what does the function of Process.dir()? Somebody told me that the dir i specified is a working directory of running process - it doesn't help to find executable. but the follow code could run rightly
@org.junit.Test
public void test() {
Process process = null;
ProcessBuilder pb = new ProcessBuilder("cmd","/c","QQ.exe");
pb.directory(new File("D:\\Program Files (x86)\\Tencent\\QQ\\Bin\\"));
try {
process = pb.start();
} catch (IOException e) {
e.printStackTrace();
}
}
The qq.exe is not in path。Who can tell me why? I am a chinese. I am not good at english, so Please excuse this wretched apology for my english.
Upvotes: 0
Views: 163
Reputation:
ProcessBuilder.directory(java.io.File)
define the working directory of the process, not the "launch", so:
case 1: QQ.exe
is started with working directory D:\\Program Files (x86)\\Tencent\\QQ\\Bin\\
but QQ.exe
is not found (not in %PATH%
)
case 2: cmd.exe
is started with working directory D:\\Program Files (x86)\\Tencent\\QQ\\Bin\\
then QQ.exe
is launched and found (cmd is in %PATH%
and QQ.exe
in current working dir)
We can assume that ProcessBuilder
starts the process then perform a working directory change.
Upvotes: 1