tags:

views:

129

answers:

4

I want to call Java from PHP 5.2, running either on a webserver or from a command line script.

From PHP 4, this seems to be simple, and just involves installing the PECL Java extension.

Example code from the PHP 4 extension:

<?php
  // get instance of Java class java.lang.System in PHP
  $system = new Java('java.lang.System');

  // demonstrate property access
  echo 'Java version=' . $system->getProperty('java.version') . '<br />';
?>

However, this extension doesn't exist on PHP 5.

What is the closest alternative for PHP 5?

edit:

Really I'm looking for an interface similar to either a C PHP extension or to that provided by the PHP 4 Java extension. The Java program is fairly small and only needs to retain a small amount of state between calls and doesn't need to run asynchronously. The PHP script would only be running a small number of instances simultaneously.

This would also need to be deployed to multiple machines (running Ubuntu 9.x and Debian Lenny), so it should be simple to install.

A: 

Now quite what you asked for, but you could have a look at Caucho's Resin, and in particular their Quercus which is a 100% Java implementation of PHP, it allows integration of Java and PHP.

rsp
Thanks, but yeah I don't want to change my PHP environment that radically to achieve this.
therefromhere
That's the point: You don't have to.
Willi
+2  A: 

This project seems to be a good bet: http://php-java-bridge.sourceforge.net/pjb/

therefromhere
+1 for this; I've used it before and for me it was a pig to set up but eventually seemed to work OK. Didn't make for extremely readable code though...
richsage
A: 

I have a project that uses a Java program to fetch some data. It's quite simple, but I found that

 $java_command="cd /var/java_dir && java my_java_program $myparam1 $myparam2";
 $result=exec($java_command,$output,$return_code);

works just fine.

Alex JL
It's a possibility, but it's a little bit crude, and I'd like to be able to store some state between calls.
therefromhere
Sure, for that I write to a database or a file. I'm sure there are more sophisticated solutions out there.
Alex JL
+1  A: 

Since you want to keep state at the java site I'd either use a java process that listens on a plain socket or use a simple embedded webserver (winstone or jetty) if you are more fluent writing servlets. Some other possibilities are listed at this related question: http://stackoverflow.com/questions/165945/what-is-the-best-approach-for-ipc-between-java-and-c

Simon Groenewolt