2

i'm trying to execute a SOX command from java, but unfortunately its returning an error everytime. Every other SOX commands are working perfectly though!! Here is the code :

class Simple {
    public static void main(String args[]) throws IOException, Exception {
        Process p;
        BufferedReader br;
        String co = "sox speech_16.wav -p pad 0 2.5 | sox - -m speech_16.wav speech_output.wav";
        p = Runtime.getRuntime().exec(co);
        br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            int returnCode = p.waitFor();
        System.out.println("reurn code : "+returnCode);         
    }
}

When I'm executing the same sox command in terminal, its working fine. I really can't understand what the problem is!! Is it because of the '|' symbol??

Sujay
  • 6,753
  • 2
  • 30
  • 49
sree127
  • 421
  • 3
  • 9
  • 25
  • Its returning 1. Actually it should return 0. – sree127 Sep 11 '12 at 07:01
  • and what does a return code of 1 from sox mean (check the man page)? – codebox Sep 11 '12 at 07:04
  • @codebox means there is some error in its output or the 'output-file' has not been generated! According to the above command it should generate a file called "speech_output.wav". – sree127 Sep 11 '12 at 07:09
  • @codebox The problem is not with the command! i think [I'm not sure] the problem is with the String when it get parsed!! – sree127 Sep 11 '12 at 07:11
  • possible duplicate of [How to make pipes work with Runtime.exec()?](http://stackoverflow.com/questions/5928225/how-to-make-pipes-work-with-runtime-exec) – josefx Sep 11 '12 at 07:25

2 Answers2

9

The issue is that Runtime.exec() does not understand shell concepts such as "|". Instead try:

Runtime.getRuntime().exec("/bin/sh", "-c", co);

The problem is that exec runs a binary directly without invoking the shell. The "|" character is only recognized by the shell, not by sox. The "-c" tells the shell to run a single command, and passes the entire command as the single argument.

epsalon
  • 2,294
  • 12
  • 19
  • It's not the directory path - it's the pipe that's the problem: http://stackoverflow.com/a/5928316/184998 – Adrian Cox Sep 11 '12 at 07:22
  • But i really need to include '|' for working. Is there any way to include that? – sree127 Sep 11 '12 at 07:23
  • yeah! it worked!! thanx @AdrianCox . It was because of the pipe symbole. Here is the code : String[] co = { "/bin/sh", "-c", "sox speech_16.wav -p pad 0 2.5 | sox - -m speech_16.wav speech_output.wav" }; p = Runtime.getRuntime().exec(co); – sree127 Sep 11 '12 at 07:31
  • @epsalon can you elborate on `-c` argument, please? – mtk Sep 11 '12 at 07:42
  • I added more detail to the answer. Hope this clears stuff up. – epsalon Sep 11 '12 at 17:26
-1

This is likely to be related to the environment in which the commands get executed, it could be any of the following:

  • The sox executable cannot be found (put the full path in the command)
  • The user does not have permission to run the sox command (check execute bit in file permissions)
  • Some environment variable needed by sox is not initialised when you run the command from Java (check sox documentation)
  • If speech_16.wav is an input file to sox then the file cannot be found (add full path of .wav file to command)
  • If sox needs to create an output file then it does not have permission to do so, either due to directory permissions, of because there is an existing file with that name which cannot be overwritten, or due to lack of space on the file-system.
codebox
  • 19,927
  • 9
  • 63
  • 81
  • It worked finally. It was because of the pipe symbol in the command. Thanks for your help btw :) – sree127 Sep 11 '12 at 07:34