tags:

views:

17

answers:

1

Hi, I have a jar file of my application which has more than one class. The jar file is called by PHP through the command prompt. I am using the following PHP snippet to call the jar file.

<?php
$result=popen('java -jar D:\\Development\\Filehandler\\dist\\Filehandler.jar getConfigLang', "r");
while(!feof($result)){
  print fread($result, 1024);
  flush();
}
fclose($result);
?>

The problem here is interesting. I am able to get the 'System.out.println' statements which are in the main function. But unable to get the output statements from other classes.

I have tried with exec() also. The .jar is working fine and when called from the command prompt directly, its working fine.

Is there a way to capture the whole output?

A: 

Have you tried using proc_open instead:

http://au.php.net/manual/en/function.proc-open.php

it allows you to setup the pipes "Will be set to an indexed array of file pointers that correspond to PHP's end of any pipes that are created."

Example from php.net

<?php
$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
   2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);

$cwd = '/tmp';
$env = array('some_option' => 'aeiou');

$process = proc_open('php', $descriptorspec, $pipes, $cwd, $env);

if (is_resource($process)) {
    // $pipes now looks like this:
    // 0 => writeable handle connected to child stdin
    // 1 => readable handle connected to child stdout
    // Any error output will be appended to /tmp/error-output.txt

    fwrite($pipes[0], '<?php print_r($_ENV); ?>');
    fclose($pipes[0]);

    echo stream_get_contents($pipes[1]);
    fclose($pipes[1]);

    // It is important that you close any pipes before calling
    // proc_close in order to avoid a deadlock
    $return_value = proc_close($process);

    echo "command returned $return_value\n";
}
?>
ozatomic