Reputation: 21
I need to execute a command line program with 2 arguments. However, it must execute in the working directory. i.e. "command arg1 arg2", not "c:\folder\subfolder\command arg1 arg2"
From other questions here, I've gotten up to using Runtime.exec(cmdArray, null, workingDirectory); but I keep getting "CreateProcess error=2, The system cannot find the file specified". I've checked, and both the path and file exist, so I don't know what is going wrong. Here is the code I'm using.
String [] fileName = {"mp3wrap.exe", "Clear_10", "*.mp3"};
String dirName = "E:\\Music\\New Folder\\zz Concatinate\\Clear_10";
try {
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(fileName, null, new File(dirName));
BufferedReader input = new BufferedReader(new InputStreamReader
(pr.getInputStream()));
String line = null;
while ((line = input.readLine()) != null) {
System.out.println(line);
}//end while
int exitVal = pr.waitFor();
System.out.println("Exited with error code " + exitVal);
}//end try
catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}//end catch`
I'm getting this error:
java.io.IOException: Cannot run program "mp3wrap.exe" (in directory "E:\Music\New Folder\zz Concatinate\Clear_10"): CreateProcess error=2, The system cannot find the file specified
Upvotes: 2
Views: 2139
Reputation: 36229
Give the whole path to mp3wrap.exe.
Java doesn't use the PATH to find mp3wrap.
-- Update after comment:
Okay - rereading the question, he asks how to start the program from inside the directory. If the program needs it, you have to start the Java program while being in this directory.
You might still have to give the whole path to the program, or start it with an indication to search for it in the current dir. I remember, that in Windows, the current dir is always searched. Other system differ here, so you would indicate the current dir with a dot, which works on Windows too: "./mp3wrap".
Upvotes: 4
Reputation: 656
Alternatively you might want to try using ProcessBulder.start()
. You can set env variables, the working directory and any args you want to pass to the Process
that is spawned by the start()
method. Look at the Java docs for a sample invocation.
Upvotes: 2