I am working on project where i need to execute jar file with PHP, i am using php exec function to execute jar file, When i am executing my php script from browser i am getting expected output, but when executing from command line i am getting error “Unable to access jarfile wrapper.jar”.
My php script having following code :
exec("java -jar wrapper.jar $arr[0] $arr[1] $arr[2] $arr[3] $arr[4] $arr[5] $arr[6] $arr[7] $arr[8] $arr[9]",$output);
PHP script and jar file wrapper.jar both are in same directory, Please check output below :
-
When executed from browser
Array ( [0] => REFNUM : 1258740043092402176 [1] => ANumber : null [2] => Status : y [3] => Txn : cf77bd77-f365-475f-b9ba-814f16f3a123 )
which is expected output
-
When executed from command line
Error: Unable to access jarfile wrapper.jar
OUTPUT: Array
(
)
as it is CLI, i also change my code to provide absolute path to jar file as per below :
exec("java -jar 'D:xampphtdocsAVAULTwrapper.jar' $arr[0] $arr[1] $arr[2] $arr[3] $arr[4] $arr[5] $arr[6] $arr[7] $arr[8] $arr[9]",$output);
i got following output :
Error: Unable to access jarfile wrapper.jar OUTPUT: Array ( )
I have also checked exec('java -version',$output)
, it shows me correct java version installed on my system on both browser and command line
I have also noticed when used single quote and double quotes in exec function it behaves differently
with double quotes (in broswer)
exec("java -jar wrapper.jar $arr[0] $arr[1] $arr[2] $arr[3] $arr[4] $arr[5] $arr[6] $arr[7] $arr[8] $arr[9]",$output);
output :
Array ( [0] => REFNUM : 1258740043092402176 [1] => ANumber : null [2] => Status : y [3] => Txn : 01bac91a-9dd5-4886-9ca1-2166233af827 )
with single quotes (in broswer)
exec('java -jar wrapper.jar $arr[0] $arr[1] $arr[2] $arr[3] $arr[4] $arr[5] $arr[6] $arr[7] $arr[8] $arr[9]',$output);
output :
Array ( [0] => ERROR : MISMATCH_AC_SA [1] => Status : n [2] => ANumber : Null [3] => Txn : ec883e3f-e8e7-4feb-ac37-9ce9f36d3d0b )
I don’t know why different behavior whith single and double quotes.
However my actual requirement is to execute jar file from php command line.
I am really confused
- Why while executing jar file from command line which is in same directory as php script, i got unable access jar file error even with absolute path to jar file
- Why exec function behaves differently with single quotes and double quotes