1

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.

tereško
  • 58,060
  • 25
  • 98
  • 150
John Carter
  • 53,924
  • 26
  • 111
  • 144

5 Answers5

3

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

John Carter
  • 53,924
  • 26
  • 111
  • 144
  • +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 Dec 26 '09 at 17:43
2

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: What is the best approach for IPC between Java and C++?

Community
  • 1
  • 1
Simon Groenewolt
  • 10,607
  • 1
  • 36
  • 64
0

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
  • 23,135
  • 6
  • 55
  • 69
0

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.

JAL
  • 21,295
  • 1
  • 48
  • 66
  • It's a possibility, but it's a little bit crude, and I'd like to be able to store some state between calls. – John Carter Dec 26 '09 at 18:26
  • Sure, for that I write to a database or a file. I'm sure there are more sophisticated solutions out there. – JAL Dec 27 '09 at 21:30
0

Zend Server also has a built-in daemon allowing you to access Java functionality from within PHP. It works right out of the box.

Kevin Schroeder
  • 1,296
  • 11
  • 23