1

I have made a .jar file that joins two wave files together.I call it with the command prompt like so.

java -jar WaveAppender.jar 1.wav 2.wav out.wav

I am trying to use php now to execute this .jar file,but the code below does not seem to work

$theFiles = Array("1.wav","2.wav","output.wav");
exec("java -jar WaveAppender.jar $theFiles");

I do not get any errors but the out.wav is not written.

Am I calling exec() wrong?

james
  • 2,595
  • 9
  • 43
  • 70

2 Answers2

4

You can't use arrays directly like that in a string. The resulting command line that would generated would be:

java -jar WaveAppender.jar Array

If what you actually want is

java -jar WaveAppender.jar 1.wav 2.wav 3.wav

then you need to do this:

exec("java -jar WaveAppender.jar " . implode (' ', $theFiles));
GordonM
  • 31,179
  • 15
  • 87
  • 129
  • If you want to properly pass arguments into Java's `argv`, you'll want to implode with a space, not a comma. – Zach Rattner Jul 02 '11 at 06:01
  • I am getting an error with that code.Parse error: syntax error, unexpected T_VARIABLE in C:\wamp\www\drum\index.php on line 4.Line is the exec() code you gave me above. – james Jul 02 '11 at 06:03
  • Here is the complete code.$theFiles = Array("bass.wav","hihat.wav","output.wav"); exec("java -jar WaveAppender.jar ". implode(',' $theFiles)); – james Jul 02 '11 at 06:04
  • Use `implode(',', $theFiles)`. – Zach Rattner Jul 02 '11 at 06:10
  • @Zach Rattner: Updated answer to fix that. – GordonM Jul 02 '11 at 06:15
  • It gives no error now.But the out.wav is not written to the directory.I try with command line and it works,just not with exec()? – james Jul 02 '11 at 06:22
3

There are several things to keep in mind here:

  1. Many hosting providers consider exec to be a dangerous function call. For this reason, it may not be available on your server. For more information on checking whether exec is enabled on your system, see this discussion.

  2. Your files are stored in an array. Given the code you posted, you are actually passing this string to exec:

    java -jar WaveAppender.jar Array

    To fix this, try using implode to concatenate all elements of the array into a string, like so:

    exec('java -jar WaveAppender.jar ' . implode(' ', $theFiles));

    For more information on implode, see the PHP docs.

  3. Remember that exec returns a value, and you can also pass in an array to be filled with all the output of the program. This would be useful for handling errors in your web app. For more information on exec, see the PHP docs.

Community
  • 1
  • 1
Zach Rattner
  • 20,745
  • 9
  • 59
  • 82