javaphpjar

XAMPP PHP EXEC function provides different output when executing from browser and CLI


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 :

  1. 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

  1. 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:\xampp\htdocs\AVAULT\wrapper.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

  1. 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
  2. Why exec function behaves differently with single quotes and double quotes

Solution

  • With help of commenters, i was able to solve issue. You must be aware that using php command line, paths must be absolute, below piece of code works for me. Please note that it is necessary to escape backslashes

    exec("java -jar D:\\xampp\\htdocs\\AVAULT\\wrapper.jar $arr[0] $arr[1] $arr[2] $arr[3] $arr[4] $arr[5] $arr[6] $arr[7] $arr[8] $arr[9]",$output);
    

    Please note that if you are passing any php variables in exec function do not use single quotes, for example :

    exec('java -jar D:\\xampp\\htdocs\\AVAULT\\wrapper.jar $arr[0] $arr[1] $arr[2] $arr[3] $arr[4] $arr[5] $arr[6] $arr[7] $arr[8] $arr[9]',$output);
    

    using single quotes php variable will be treated as string,you will get unexpected output, more detail about single and double quotes in PHP can be found here