How to use Java from PHP 5

17,199

Solution 1

You will probably find tons of useless and outdated information regarding how to use Java classes from your PHP webapp which doesn't help at all. About a year ago I had faced a similar problem and I found out that the only way that is mature enough to use in production is PHP/Java Bridge -- the one you found on Sourceforge. It works better than one might expect and doesn't require knowledge of Java (however if you do know Java, you should consider writing a Web-service for this purpose and consume it on the PHP-side).

The documentation of the Bridge is not very obvious, however remember you should stick with it and not read other resources that describe different methods - don't mix things up, i.e. you don't have to install any PHP-extensions or smth -- all of them relates to another (usually outdated and unsupported) ways to call Java from PHP.

PHP/Java Bridge is a network protocol, so the only thing you need on your PHP-side is to ensure "allow_url_include" is enabled in your php.ini

From the Java side you have to get PHP/Java Bridge bound to a free port and listen to connections. Then your PHP-app will able to do "require_once" of the Bridge's proxy file which will be available on that port. That's it and you can instantiate and call Java classes transparently from your PHP script using that proxy Bridge object (it will serialize and transfer calls and results behind the scenes).

There are two options how get the Bridge up and bind it to some local port.

1) complex one: setup a Tomcat, create a war file with the bridge itself and the library you'd like to use. This way is described here:

http://php-java-bridge.sourceforge.net/pjb/webapp.php

To deploy the war into your Tomcat installation simply put the war into Tomcat's webapp folder and restart it.

2) easier one: package a jar instead of war and run it (it will use embedded web-server, so you won't have to install Tomcat and deploy anything). To go this way refer this page:

http://php-java-bridge.sourceforge.net/pjb/desktop-apps.php

Both ways will work for you (don't look at the "desktop" word at the second option, this way will work fine with your PHP webapp as well).

Refer to examples on how to use it at the "Examples" section (obvously). Also you can refer to my small PHP client that talks to Neo4J embedded database in Java using PHP/Java Bridge. However this sample might be more complex and it was intended to be deployed to the Tomcat, anyway you can have an idea how to instantiate and use classes from it:

https://github.com/coffeesnake/neo4j-php-wrapper

Solution 2

Quercus:

Quercus is Caucho Technology's fast, open-source, 100% Java implementation of the PHP language. Quercus is a feature of Caucho Technology's Resin Application Server and is built into Resin - there is no additional download/install. Developers using Resin can launch PHP projects without having to install the standard PHP interpreter (http://www.php.net) as Quercus takes on the role of the PHP engine.

Solution 3

You could just call a simple Java app from PHP by forking a process and communicating over stdin / stdout. Alternatively, if you integrate Sphinx into a webapp you could integrate by making HTTP calls.

Share:
17,199
Nate Glenn
Author by

Nate Glenn

Language processing engineer interested in cognitive modeling, multi-lingual computing, and NLP for language learning.

Updated on June 20, 2022

Comments

  • Nate Glenn
    Nate Glenn almost 2 years

    I've looked at quite a few tutorials on using Java from PHP, but they seem to conflict with eachother. Many of them say to change php.ini to use a dll file, but apparently there are "built in" capabilities in PHP 5 now. But that requires Tomcat? Do I have to get rid of Apache to run that? http://php-java-bridge.sourceforge.net/pjb/installation.php says to put the .war file into Tomcat's autodeploy directory, but I don't see any directory called "autodeploy". Would someone explain step-by-step how to get get Java stuff working on a Windows machine (I'm using Windows 7) with PHP 5 and Apache?

  • Nate Glenn
    Nate Glenn almost 13 years
    I don't want to invoke java several times, just once and then communicate about new audio files to recognize. This is because there's lots of overhead in allocating the recognizer, but not much after that. A webapp sounds interesting, but I've never written one before...
  • Nate Glenn
    Nate Glenn about 9 years
    Neat. It's GPL, so I definitely could have used it for my research applications.