Update: The php/java bridge files needed to run this tutorial as it was created orginally can be downloaded from here( version 3.2.1) . The present version of the php java bridge does not support this windows version of the tutorial.
Here's how to team up two of the most popular and powerful platforms for web development together. With the php-java bridge you can build classes and jar files in Java and call them and use their methods in PHP. Use the quick and easy PHP language to bring Java muscle to your web pages.
In this tutorial I am using Windows 2000 Server, PHP 5.1.4 and php-java bridge 3.1.6. Windows is my operating system of choice but there is information on installing and using php-java bridge on Linux and Mac OS. You can find it under the resource link at the end of the article. I am going to assume that you have a bit of knowledge about both PHP and Java. You should know enough to get them installed and be able to do simple coding in both.
Download and install Java 5 JDK. You can use the Java 6 Beta if you like but the latest stable is jdk 1.5.0_09. I wanted to make sure my rusty Java skills were not further hindered by a beta software glitch. It's bad enough that everything I have learned in Java 2 and all my book samples bring up those nasty deprecated messages whenever I try to compile something.
Once you have installed the JDK you will need to adjust the PATHS to point to the Java installation and the set a CLASSPATH. Follow the next steps.
Wampserver is my choice of Apache2, MySQL and PHP. I find that it is easier and lighter weight than XAMPP. All too frequently I get emails about how to do things in XAMPP. The answer is, the same way as you would in Wampserver. My feeling is if XAMPP is your choice then you should take sometime to learn about it. There is just not enough difference between the two to make doing a seperate article necessary. They are both WAMP packages in the end.
This is the key to getting everything to work. The php-java bridge is a dll file. It is stable but does not seem to be documented very well. I have a feeling this will change soon but until then you need to know a few things to get it going. The first thing is that the files once downloaded and decompressed do not show. This threw me off for a while until I decided to test the functions. When I launched the test.bat file the rest of the installation files appeared.
So I copied the directory holding them over to the preset Apache htdocs directory for wampserver and surfed to the test.php that was showing. The result was a warning that I was using the wrong php-java.dll and php-java.jar file. These incorrect files were the ones that came with the installation ow Wampserver. The next step was to copy the correct files files from the php-java install directory over to the Wampserver Apache PHP directory where the PHP extensions were stored. Then I surfed to the test page again and was greeted by phpinfo screen and other information that could only come from PHP talking to Java. Very cool!
From here things get easier and easier. The code below is your sample Java code that needs to be compiled into a Java class to be called from PHP. Take the code show below and place it in a text editor and save it as "hello.java" in a directory. Compiling is easy just start up the command prompt go to the directory holding the hello.java file and give the c:> javac hello.java command. This will create a hello.class file. You can leave it there and give the path to it when coding your PHP.

For those that don't know much Java yet here's some Hello World code with a little spice to it so it does not get boring.
//------------------------------------------------------------------
import java.util.*;
import java.text.*;
import java.math.*;
public class hello {
public static void main(String[] args) {
//do nothing - this will keep us from getting a compile error
}
public String SayHello() {
Date today = new Date();
return "Hello Geek here is the java date and time: " + today;
}
public int SayNumber(){
int mynum = 0;
for(int i=1;i<=5;i++){
mynum =+ (int)(Math.random()*100);
}
return mynum;
}
} //------------------------------------------------------------------
The PHP code here is simple and straight forward. It is not OOP. But you probably got enough of that while doing the Java code.
java_require("F:\\wamp5\\www\\classes\\");
$obj = new Java("hello");
// Call the "sayHello()" method
$output = $obj->SayHello();
echo $output.' this text in PHP
'; // Displays (so this comes from the class!)
// Call the "SayNumber()" method
$output = $obj->SayNumber();
// Displays (so this comes from the class!)
echo $output.' is a lucky number';
//Because the JVM caches everything we want reset while playing with the code. Otherwise the
// a cached version of the same class file will be used rather than the new one
echo "
Resetting back-end to initial state\n";
// suppress the warning message from the use of reset.
@java_reset();
?>
That's it. Now browse to the PHP page and you should get the results shown below.

Make note of the @java_reset() function at the end of the PHP snippet. It is very important to remember to add this while testing. This is because the Java JVM caches the call to and the Java object. Without java_reset() when you update a class file you will get the previously cached one instead.
More code and tips on installation to Mac OSX and Linux OS can be found here at php.net. If you need support there is a mailing list at php-java-bridge-users@lists.sourceforge.net. Though there is little activity the core developers of the php-java bridge are there to help.
The php-java bridge is stable and should not be hard to talk a web hosting provider into installing if they have not done so already. While Java hosting is not as wide-spread as PHP with the recent changes made in the licensing this will change also. The best part of all is that this gives PHP programmers a good reason to learn Java and vice versa.
Happy Publishing!
thanks for the update…it’s been a while but every year or so I fire up the latest php-java bridge and always it flakes out. seems that they may have finally got it reliable. maybe I’ll resurrect an old project.
thanks.
I’m interesting about the life of java object loaded, can it stay alive after php request? or dead with php?
as far as i think it wud be dead just the php page execution ends as it normally happens with a php object!
The class loader is what is caching. It seems the class loader is different on each type of JVM. You can also write your own class loader. The programmers for php-java bridge use the GNU loader and provide a means for emptying the cache. If you use another type then you may have to find that loaders method for emptying the cache.
Shouldn’t the java_reset() be called in the beginning of the script? Now, if the PHP script failes, the JVM will not reset.
I don’t think it will effect the outcome much. Although if set at the beginning reset() will try and clear the cache before the PHP code calls the loader and the cache is empty on the first run.
The project team is pleased to announce a new and snazzy Web site for
the php/Java bridge project.
http://php-java-bridge.sourceforge.net/pjb/index.php
The new Web site features better organization of all of the work Jost
has compiled over the years and will include some new sections to
highlight examples of using the bridge, case studies from our user
community, and upcoming platform-specific installation guides.
We look forward to your suggestions and comments on the new site.
Get php running correctly for command line php only
and get java running too so you can compile. I used
jdk 1.5.0_11 and php 5.2.0
Download JavaBridge 4.0.1 and extract the JavaBridge.war
from the .war extract JavaBridge.jar to a folder
on your box.
in that folder do:
java -jar JavaBridge.jar SERVLET:9090
which starts up a server for you.
then use:
import java.util.*;
import java.text.*;
import java.math.*;
public class Hello {
public static void main(String[] args) {
int i; // Do nothing
}
public String SayHello () {
Date today = new Date();
return “Hello Geek, today is ” + today;
}
public int SayNumber () {
int mynum = 0;
for (int i=1; i< =5; i++) {
mynum =+ (int)(Math.random()*100);
}
return mynum;
}
}
and compile to another folder. I used
C:\Documents and Settings\root
Then code a php file:
require_once("http://localhost:9090/JavaBridge/java/Java.inc");
try {
java_require("C:\\Documents and Settings\\root\\");
$obj = new Java("Hello");
$output = $obj->SayHello();
echo java_values($output) . “\n”;
$output = $obj->SayNumber();
echo java_values($output) . “\n”;
@java_reset();
} catch(JavaException $ex) {
$exStr = java_cast($ex, “string”);
echo “Exception occured; mixed trace: $exStr\n”;
$trace = new java(”java.io.ByteArrayOutputStream”);
$ex->printStackTrace(new java(”java.io.PrintStream”, $trace));
print “java stack trace: $trace\n”;
}
?>
have fun.
b.t.w, the Tomcat server runs this ok too on the 8080
port (when you deploy the .war), but I wanted to try command line and pure the java server only.
It works w/o the dll stuff since there can be
problems between PHP and the dll versioning.
i hv follow every step u explain but still got this error :
#!/bin/env php Please permanently activate the extension. Loading java extension php_java.dll now…
Warning: dl() [function.dl]: Not supported in multithreaded Web servers - use extension=php_java.dll in your php.ini in C:\Program Files\xampp\htdocs\php-java-bridge\test.php on line 42
Please permanently activate the extension. Loading java extension java-x86-windows.dll now…
Warning: dl() [function.dl]: Not supported in multithreaded Web servers - use extension=java-x86-windows.dll in your php.ini in C:\Program Files\xampp\htdocs\php-java-bridge\test.php on line 42
Please permanently activate the extension. Loading java extension php_java.dll now… Error: Either the java extension is not installed or it was compiled against an older or newer php version. See the HTTP (IIS or Apache) server log for details.
hw can i solve this error?
help me
thanx
I don’t understand because I’m unable to make java work on my windows XP installation with wamp server running. I tried all you said, and when I launch the test.php file from within the bridge directory, the result.html file says everything is working fine, BUT when I do a phpinfo from within my www of my wamp installation, I still has no java line at all in the infos provided. Dunno what’s wrong…
sorry but can i use the php-java bridge in my php pages hosted in a server? i can modify the php.ini file, but i don’t know the java installation path
its sad that this article did not mention anything about how to configure java/php bridge. its such a pain doing it on windows.
everything is straight forward there is no configuration. What part of the tutorial was not clear?
hi all,
if any one is successfully invoking java in php.Please help how to configure in php.ini file and php extensions folder. Can any one help me. thanks
In the pho script above, don’t you need require_once(”java/Java.inc”); ?
Will it work without it? Thanks
The java.inc file contains necessary PHP code classes and methods. You could write your own but using the one you get is so much easier. You could also just copy and paste the PHP contained in java.inc to your file. But it is over 2000 lines of code. Do you really want to do that ;)
Otherwise I suppose mean that java_require is in place of require_once(java/java.inc). The explaination is that the tutorial is old. The java bridge has advanced very fast. I will try and udpate the tutorial to suit version 5.2.2
Please do a updated tutorial for 5.2.2! I’ve been trying to get it to work for a few days now, and i’m beginning to give up :/
Before you start the server using “java -jar JavaBridge.jar SERVLET:9090″ , you also need to copy Java.inc from the JavaBridge folder into the same folder that you placed JavaBridge.jar
Okay, I have spent a few hours going through the docs and setting up a test script or two. I have come to the conclusion that the Bridge is no longer Windows friendly. The reason being is that there is no dll file in the latest version. This forces you to use a Java application server like Tomcat or instantate a Java servlet as recommended in the docs. This is not at all what most want to do. Definatly not what I want to do.
So I will post a new tutorial. But I don’t think Windows users are going to like it. My suggestion is to stick with the older version or help with the testing of the php_java bridge in PHP5. Though this is an experimental dll extension of PHP5 it is still more natural than being forced to use a certain type of webserver to get Java to PHP functionality.
Now there may be some stability issues. There always are…sigh. But the way I see it the only way to see something go from experimental to a stabel version is for developers to use it and share information about it.
Don’t use WAMP :: The option in the WAMP systray interface do not correlate with PHP.ini
-Sylnsr
This one adds the following line of code:
require_once(”http://localhost:9090/JavaBridge/java/Java.inc”);
If you get the error “Call to undefined function java_require()” you must add the line above. Adding the php_java.dll will provide that function, but you can’t use the DLL *and* include the line above since they contain functions with the same name. You might (as of this writing) get a “Cannot redeclare {function name..}” error.
I strongly agree with the comment above regarding DLL versioning, and the fact that its better to have a pure Java server to run the Java.
-sylnsr
I fully agree and I would like to use the old version. Unfortunately I could not find it any more on the internet these days. I would be VERY grateful if anybody could help me to acquire it somewhere, preferably the php-java-bridge-3.2.1_ j2ee.zip
You can find 3.2.1 and other versions at sourceforge under older releases. But there is no dll file that I could find. I hope that someone with the skills will pick this up and branch it into a better version for windows. After all there are still Java programmers that develop for and on windows.
php-java-bridge version 3.2.1
Or maybe the Zend guys will get on the ball and work with Sun on getting the built in PHP dll to work with the release of PHP 5.3.
sorry to say you dear,
It is useless article that you have posted.
you should be care full what you are serving to others, coz it spreading bad impact of you forum.
Your step have killed my couple of hour without any result.
I was just looking for a solution that can help me to integrate Java with in wampserver.
–A
You might want to post the problems you are having. Just about everything has been covered, including the fact that the latest java bridge does not work on wampserver because of the missing dll.
and the point of bridging these two is what? please. give us a break
If you have no need then don’t use it! There a many others both PHP and Java programmers that need this in their projects.
But just to give an example. I have a java class running in OpenCms that I want to connect to using a WordpressMu plugin. The java-bridge makes this a simpler task.
Warning: dl() [function.dl]: Not supported in multithreaded Web servers - use extension=java-x86-windows.dll in your php.ini in C:\Program Files\xampp\htdocs\php-java-bridge\test.php on line 42
Please permanently activate the extension. Loading java extension php_java.dll now… Error: Either the java extension is not sohbet installed or it was compiled against an older or newer php version. See the HTTP (IIS or Apache) server log for details.
If you are going to be doing this type of developement you need to also keep up with the news and announcements concerning PHP. As of PHP 5 dl() is no longer being used. The actual error tells you how to fix the problem.
Also posting an error message means nothing without you posting any details of how and when it occured. What are you trying to accomplish with your comment?
Please give me detail how configure
java bridge in (php-javabridge_4.2.0_j2ee.zip)in WAMP server
i use php-java-bridge_5.3.4.1 but can’t see some files in cgi when extracting JavaBridge.war
I set classpath in php.ini file.
but still not working..
Please……….
Plese give me the screen shot…..and help …….please……….
The java bridge is only compatible with with windows up to version 3.2.1. There are no compiled dlls for windows past this version.
For versions after 3.2.1 you will have to use the native java ( built in application server )solution which is documented in the down load of the packages.
Nice article thanks.
Lost
I’m trying to call php from a remote java server and have run into a wall.
The javadoc posted on the site shows a method in the EngineFactory called getInvocablePhpScriptEngine that takes a URI as input, but this method is not implemented in the distribution!?
I have downloaded the php-java-bridge_5.4.3.1_j2ee version (since I could not find a war in the later versions) and included the php-servlet.jar in my classpath.
What am I missing, anybody have an idea?
Regards
/K
For those of you having trouble with the java bridge after version 3 ie, version 5.xx please try and get support or help first from the mailing list.
This tutorial and my present work only are on the 3.2.1.1 bridge which is a windows dll. Later version are implementations that use different techonolgy.