2

I am creating a command string by adding some strings together. If i debug my application and copy final string from variable it works perfectly in terminal. If try Runtime.getRuntime().exec(cmd1); where cmd1 is my string it doesn't work, i get error 2 (no such file or directory).
My code looks like this:

String cmd1 = sPath + " \"" + files[i].getPath() + "\" \"" 
+ files[i].getPath().replace(".wav", "_L.wav") + "\" remix 1";
Process p1 = Runtime.getRuntime().exec(cmd1);
p1.waitFor();

The final cmd1 string is this:

 /Users/Me/Downloads/sox-14.4.1/sox "/Users/Me/Desktop/DB/A1199/Klu a1.wav"
 "/Users/Me/Desktop/DB/A1199/Klu a1_L.wav" remix 1

Any ideas why i get this error? I tried putting sox path into quotes but it doesn't help.

Ok, i tried this:

String[] cmd1 = new String[4];
cmd1[0] = soxPath;
cmd1[1] = "'" + files[i].getPath() + "'";
cmd1[2] = "'" + files[i].getPath().replace(".wav", "_L.wav") + "'";
cmd1[3] = "remix 1";

ProcessBuilder builder = new ProcessBuilder(cmd1);
builder.redirectErrorStream(true);
System.out.println(builder.command().toString());
final Process p1 = builder.start();
copy(p1.getInputStream(), System.out);
p1.waitFor();

But i still get same results... command works in terminal, but java app throws an error: /Users/Me/NetBeansProjects/DataPrepare/sox/sox FAIL formats: can't open input file "/Users/Me/Desktop/DB/audio.wav"': No such file or directory

JNM
  • 1,175
  • 4
  • 20
  • 39
  • Consider using a [ProcessBuilder](http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html). `Runtime.exec()` [doesn't work](http://www.javaworld.com/jw-12-2000/jw-1229-traps.html). At least you will be able to get what is printed on stderr. – fge Jun 16 '13 at 18:22
  • I tried using `ProcessBuilder`, but i get the same problem. – JNM Jun 17 '13 at 04:43
  • Have you redirected stdout/stderr to a file? If yes, what does this file say? – fge Jun 17 '13 at 04:49
  • Read (and implement) *all* the recommendations of [When Runtime.exec() won't](http://www.javaworld.com/jw-12-2000/jw-1229-traps.html). That might solve the problem. If not, it should provide more information as to the reason it failed. Then ignore that it refers to `exec` and build the `Process` using a `ProcessBuilder`. Also break a `String arg` into `String[] args` to account for arguments which themselves contain spaces. – Andrew Thompson Jun 17 '13 at 06:02
  • @fge I disagree with your implication that `exec` "doesn't work". As we both should know from reading that article, it *does* work, but has many traps. `ProcessBuilder` is better, but can also fail if those tips are ignored. – Andrew Thompson Jun 17 '13 at 06:03
  • @AndrewThompson I compare using `Runtime.exec()` instead of a `ProcessBuilder` a little like using `system()` in C instead of `exec*()` -- although technically this isn't really true (`Runtime.exec()` doesn't fork a shell), I see no reason to use the unreliable way ;) – fge Jun 17 '13 at 06:05
  • @fge I agree with the basic thrust of what you are saying, but "doesn't work" is not the same as "unreliable" or sub-optimal. I would only use the last term to describe `exec`. – Andrew Thompson Jun 17 '13 at 06:54
  • @fge: `Runtime.exec()` and `ProcessBuilder` are entirely equivalent (look at your favorite JDKs implementation of `Runtime.exec()`, chances are it uses `ProcessBuilder` internally). It "only" differs in that `Runtime.exec()` is a more convuluted, harder-to-understand API. But the pitfalls are pretty much the same. – Joachim Sauer Jun 17 '13 at 07:31
  • @JoachimSauer I don't really call "entirely equivalent" an API which allows me to alter the environment, std{in,out,err} etc and one which doesn't... – fge Jun 17 '13 at 07:35

0 Answers0