1

I want to call a java program and fetch it's output in stdout. I followed the suggestions in stackoverflow. But it doesn't work.

I have add the class file to my CLASSPATH. And I can execute the command in cmd correctly as follows:

enter image description here

In my PHP file I call this program by

exec("java Hello", $output);
print_r($output);

It yields nothing but:

Array()

What is the problem? How can I fix this?

ps: Hello is a demo program, actually the program I want to call is much more complicated which might take 2 or more seconds in my machine(i5 4G).

Community
  • 1
  • 1
Joey.Z
  • 4,492
  • 4
  • 38
  • 63
  • Try and see what `passthru()` gives back (instead of `exec()`). `exec()` might be selective sometimes to catch only returned values rather than printed content. Use `escapeshellarg()` to secure your script against hacks. –  Apr 07 '13 at 08:14
  • @Allendar passthru returns the return value? Anyway, the browser gives `1`. – Joey.Z Apr 07 '13 at 08:18
  • If the `main()` function in your JAVA program returns 1 (return 0 = no errors, 1 = somethings wrong ;)) it means there was an error. It might have something to do that PHP's commandline doesn't fetch your JAVA the same way as you do in `cmd`. Both are of course different settings-wise. Like `cakil` answered it might work to loop on the Array() you got back, as echo/print in PHP won't show the inners of an Array by default, like you might be used to in other languages. –  Apr 07 '13 at 08:22
  • @Allendar thanks, I checked the returned Array carefully to ensure that I didn't miss anything, but it IS empty. And `passthru` actually gives nothing intead of `1`, I said `1` because I mistakenly set the class. Anyway, thank you. And if this won't work anyway, I can alternatively try to wrap this in jsp file or some other server-script language. I just want to make it on-line such that users can access the processed data yield by the java program. The worst case is that I have to translate the java program to PHP or other server-script language... – Joey.Z Apr 07 '13 at 08:31
  • Try to just put `return 0;` in your JAVA class, at least to test if `passthru()` is not always giving back 1. It should read RAW-input, so it's really something with calling JAVA from the PHP script. Maybe (to test) try execute JAVA from it's absolute path too. The more you test, the more you will mostly get to know :) –  Apr 07 '13 at 08:42
  • @Allendar eh..I just find out that you can't returns a value from main in Java. It must have a signature like public `static void main(String[] args)` – Joey.Z Apr 07 '13 at 08:54
  • Yet it still exits with the code `1, which means something goes wrong when calling the JAVA application from PHP's shell. –  Apr 07 '13 at 08:57

4 Answers4

2

I would recommend using Java/PHP Bridge found here: http://php-java-bridge.sourceforge.net/pjb/ It's quite easy to install and works very well.

Also, I recommend using the following link to download it. (it's the same one as the link in downloads->documentation)

http://sourceforge.net/projects/php-java-bridge/files/Binary%20package/php-java-bridge_6.2.1/php-java-bridge_6.2.1_documentation.zip/download

The file is JavaBridge.war. You'll probably want to use Tomcat for the Java EE container. Once Tomcat is set up, you just put this file in the webapps folder and it's installed.

If you want to regularly use java classes in PHP this is the best method I know of and I have tried a lot of them. Resin also worked, but it didn't play nice with my mail server.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
starshine531
  • 601
  • 5
  • 19
1

Try this:

exec('java -cp .:/path/to/folder/of/your/file Hello 2>&1', $output);
print_r($output);

the 2>&1 need to display errors.

Footniko
  • 2,682
  • 2
  • 27
  • 36
0

Well, it yields array right? so instead print_r($output) try print($output[0]), that outputting 'Hello World' on my console :D

oentoro
  • 740
  • 1
  • 11
  • 32
  • Actually it yields empty array, cuz `print_r()` prints recursively what is in the array. And I did try print($output[0]), which also gives nothing. – Joey.Z Apr 07 '13 at 08:23
  • Well, I'm curious about the content of your hello world program. Mine is simple just `System.out.print("Hello World");`, in main procedure – oentoro Apr 07 '13 at 08:29
  • me too. And as you see in the figure above, it runs good in cmd, and gives the right result. – Joey.Z Apr 07 '13 at 08:41
  • Hmm, it has same problem as you, and not solved yet: http://stackoverflow.com/questions/12448915/php-exec-tesseract-outputs-empty-array maybe some kind of bug, or try with higher permission while executing php? – oentoro Apr 07 '13 at 09:03
0

try pipe

$command = 'java Hello';
$descriptorspec = array(
    1 => array(
        'pipe', 'w'
    )
);
$process = proc_open($command, $descriptorspec, $pipes);
if (!is_resource($process)) {
    exit("failed to create process");
}
$content = stream_get_contents($pipes[1]);
fclose($pipes[1]);
if (proc_close($process) === 0) {
    print_r($content);
}else{
    exit("failed to execute Hello");
}
Peng Qi
  • 1,412
  • 1
  • 10
  • 13