PHP with java

2004 May 20 at 07:08 » Tagged as :

I have read countless posts by users on the php mailing lists from people having trouble installing the java extension for PHP. I couldn't understand what all the fuss was about.

In case you are new to PHP, the java extension is a system that allows you to make use all the power of the java programming langauge from your PHP script. While PHP is leagues ahead of java in many aspects when it comes to web applications, java is a multipurpose langauge that can be used in many areas that you can't even dream of using PHP.

To get back to the topic, I couldn't be bothered with recompiling PHP from scratch just to install an extension so I changed into the ext/java folder in the php source tree and executed the following commands.

phpize

./configure

make

make install

This installs the php extension. Not to to configure it.

I used java version 1.4.2. The configuration directives in the README are for ancient versions (but it's still a very very good document), if you are using any of these version you might as well go back to using DOS, windows 95 or Red Hat 6.0

Find out where you have your java installation, and if you don't already have the JAVA_HOME enviorenment variable set up do so now. (export JAVA_HOME=/path/to/java/). Make sure to add this to your start up scripts so that it is aavailable for any application.

in my case I have java installed in /opt/java/j2sdk1.4.2/ So my php.ini file looks like this

java.class.path =/usr/lib/php4/php_java.jar

java.home = /opt/java/j2sdk1.4.2

extension_dir=/usr/lib/php4/

extension=java.so

Now once you restart the system you will find that the java modules is indeed installed ok but when you run your first script (may be the jver.php sample in provided in the php distribution) you will run into the following error message

Fatal error: Unable to load Java Library /opt/java/j2sdk1.4.2/jre/lib/i386/libjava.so, error: libjvm.so: cannot open shared object file: No such file or directory in /var/www/html/tests/java.php on line 4

This particular error could be overcome by adding another configuration directive

java.library.path = /opt/java/j2sdk1.4.2/jre/lib/i386

Now everything should work like clockwork unfortunately it does not. I now get this error

Fatal error: Unable to load Java Library /opt/java/j2sdk1.4.2/jre/lib/i386/libjava.so, error: libverify.so: cannot open shared object file: No such file or directory in /var/www/html/tests/java.php on line 4

I updated my ld.so.conf and ran ldconfig and then went through the configure, make and make install steps as well. Guess what? still the same error. Now I know what all the fuss is about.

footnote (July 09, 2004) :

The main site now has an article on this topic