PHP Java Bridge

Why You Might Use It

PHP is an excellent language. It can be quick and easy to get something running; there is enough structure within the language to write maintainable code. It does, though, suffer from a couple of problems: it’s slow and it’s single threaded. 90% of the time this doesn’t matter. Most web applications do not do a lot of number crunching and profiling usually shows database access and network transfer time as the main culprits of slow applications.

Sometimes, though, you need more horse power. For example, we want to be able to search 600,000 documents in less than a second. Our PHP code was taking from 30seconds up to 10minutes – depending on the complexity of the search. In Java the same searches took less than a tenth of a second.

Another reason for using Java is to access the Java libraries. There are some excellent Java classes for creating Excel files, PowerPoint and working with PDFs. Yes, it is possible to do this in PHP. We’ve worked quite a bit with Excel classes in PHP and written our own Word Document classes but it is very clumsy, slow, and you can watch the memory leak out of the back of the machine. If it’s simple why not just use the Java classes from your PHP application?

What It Is

There are two parts to the bridge: a Java application and a set of PHP classes. The Java application runs either on it’s own listening to a particular port or it can run from a Tomcat server. The bridge can call PHP from Java or Java from PHP. I’m going to concentrate on calling Java from PHP as that, for me, is the most interesting use case.

The PHP libraries need to be included in the PHP application. When you instantiate a Java class or call a Java method, the PHP creates XML which describes the call and writes the XML to the port the Java application is listening on. The Java application returns XML which the PHP reads from the port. The XML describes the result of the call and returns the result to the PHP.

A Worked Example

To demonstrate how the bridge works I’m going to write a hello world style program that instantiates a Java class, called Parrot, calls the method setting  a private variable, then calls another method to return that variable which is echoed out.

Get the bridge

Download the bridge from http://sourceforge.net/projects/php-java-bridge/files/Binary%20package/php-java-bridge_6.1.2.1_documentation.zip and unzip it. Installation complete.

The Java Parrot

Create a file call Parrot.java containing

public class Parrot {
String message;
public void setMessage(String aMessage) {
message=aMessage;
}
public String getMessage() {
return message;
}
}

public class Parrot {        String message;

public static void hello() {                System.out.println(“Hello from Java”);        }

public void setMessage(String aMessage) {                message=aMessage;        }
public String getMessage() {               return message;        }
}

Compile the parrot and create a jar file. Of course, the Jar file could have many classes than this simple parrot.

javac Parrot.java

jar cvf parrotLibrary.jar Parrot.class

Start The Bridge

The file JavaBridge.jar is contained in the binary package downloaded above. Running it is a simple as

java -jar JavaBridge.jar

There are a few parameters you’ll probably want to set.

The port number you want to listen to. Of course this should be unused on your system and match the port number we’ll set in the PHP below

We want to run the bridge as a daemon

The log file and log level. A reasonable log level is 3, though if you set this higher you cna see the XML communication between PHP and Java.

java.ext.dirs: the location that the bridge will look for classes. This defaults to lib/ext in the JDK directory structure and /usr/java/packages/lib/ext. The parrotLibrary.jar can either be copied to /usr/java/packages/lib/ext or you can add another directory to the path.

The basic usage of the bridge is

java -jar JavaBridge.jar PortNumber Loglevel Logfile

Other parameters can be passed in using -D on the Java command.

We’ll run the bridge using (presuming the JavaBridge.jar is in your current directory)

java -Dphp.java.bridge.daemon=”true” -Djava.awt.headless=”true” -Djava.ext.dirs=/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/ext:/usr/java/packages/lib/ext:. -jar JavaBridge.jar 8087 3 bridge.log

This runs the bridge as a daemon, adds the current working directory to the path that the bridge will look for jar files, listens on port 8087, sets the log level to 3 and logs to the file bridge.log.

Call the Java from PHP

Now create a PHP file, Parrot.php containing

<?php

define(‘JAVA_SERVLET’,false); //Not using a web based interface

define(‘JAVA_HOSTS’,8087); //The port on which the Java App is running

require_once(‘java/Java.inc’); //the Java bridge

//Test a standard library call

echo new java(“java.lang.String”, “hello world”);

echo PHP_EOL;

//Try our Parrot

$Parrot=new java(“Parrot”);

//Set a parameter on the Java class

$Parrot->setMessage(‘Pieces of 8’);

//Get the value back

echo $Parrot->getMessage().PHP_EOL;

The files in the java directory should have been unpacked when the downloaded bridge file was uncompressed. The PHP above sets the port number to match that set on the Java file and tells the included files not to call the Java over a web interface – this would be used if you use Tomcat as the Java server.
The PHP itself tries a standard Java library call to demonstrate the Java connection.
The Java parrot is instantiated, we call a method setting a private variable; call another to return the value of that variable and echo the result.
Run the PHP by
php Parrot.PHP
You’ll get the output:
hello world
Pieces of 8

Further Information

Documentation is available in the ZIP file downloaded above, including information on how to call PHP from Java. There is a readme file for each of the standard use cases.

There is an FAQ, details of the API, instructions for working with Eclipse, a user contributed installation video amongst much else available on the php/Java bridge sourceforge site:

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

  1. No comments yet.

  1. No trackbacks yet.