PHP/Java Bridge – Ubuntu Server 7.10
After spending the last few weeks learning PHP, I have decided to combine this knowledge with my other proficient language – Java. How? Via PHP/Java Bridge available at http://php-java-bridge.sourceforge.net/pjb/.
This tutorial will explian the uses of the PHP/Java Bridge, some example code and a breif how to for installing it on an Ubuntu Server 7.10.
If you navigate to the projects website, it states that “The php/java bridge allows you to quickly access java classes from within your PHP scripts without having to know Java. It also allows you to access PHP scripts from within your Java classes without having to know PHP.”
Why might you want to do this? Well, it allows more functionality and flexability within your applications. Here is some example code of php/java bridge being utilizied. (Please note that I do not explain either language, it is assumed that you already know them)
public class phpJavaExample{
public static void main(String[] args){
//Don't need a main method as we are calling it from php, this is only for compiling issues.
}public String flyNinja() {
String ninja = “FlyNinja is a great resource for all your intellectual, technological and compulsive needs.”;
return ninja;}
}
This bit of code above is a simple java class, with one single method which returns a string when called from the php.
< ?phpjava_require(“/var/www/javaphp/classes/”); //this is the location of your java class
$javaObject = new Java(“phpJavaExample”); //This calls the java class file in php.//calls the java method flyNinja()
$fly = $javaObject->flyNinja();
echo ‘This is what is returned from the flyNinja method: ‘ . $fly;@java_reset();
?>
The php file is simply creating an instance of the java class, calling a method which is returning an object. The object is then stored in a php variable, which is then printed on the screen. Here is a screenshot of the final product.

How to install PHP Java bridge on Ubuntu Server 7.10:
First you must install the following java files on your system if you have not done so already.
sun-java6-jre
sun-java6-fonts
sun-java6-jdk
sun-java6-plugin
Next, you must download the PHP Java Bridge deb files from sourceforge, head over there to grab the latest source.
Remember the directory name to which you downloaded the .deb files to and use the following commands:
sudo apt-get install liblucene-java libitext-java
sudo dpkg -i php-java-bride_*.deb (where * is your currently version)
Finally, you can restart apache (sudo /etc/init.d/apache2 restart) and you are ready to go. Use phpinfo() to double check if everything is installed correctly.
