Executing a java .jar file with php
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开发者_如何学Go is not written.
Am I calling exec() wrong?
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));
There are several things to keep in mind here:
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 whetherexec
is enabled on your system, see this discussion.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.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 onexec
, see the PHP docs.
精彩评论