Greg's Blog

helping me remember what I figure out

PHP and Servlets - Part I

| Comments

It was my intention from the outset to build this site using PHP, however as I was working my way through building the CMS and the front end that it would be cool to build certain parts of the functionality in Java or maybe all of it? I decided to proceed along the PHP path first, after all it was nearly done and I might gaina few more insights along the way. But now that this done I figured it was time to delve a little deeper.

I’ll be looking into setting up the PHP environment first. Most of the following will concern itself with editing the php.ini file. I am assuming that you have downloaded and installed the JDK. Without it, the whole exercise is a little pointless. OK, open edit the php.ini (usually located in a Windows based system in your Windows system folder). Look for segment that looks like the following:

[Java]
;java.class.path = .\php_java.jar
;java.home = c:\jdk
;java.library = c:\jdk\jre\bin\hotspot\jvm.dll
;java.library.path = .\

The ini file needs to be modified to point to the class path, the java home, the java virtual machine and finally the location of the Java extensions.

[Java]
java.class.path = “C:\PHP\extensions\php_java.jar;”
java.home = “C:\j2sdk1.4.0_01\bin”
java.library = “C:\j2sdk1.4.0_01\jre\bin\server\jvm.dll”
java.library.path = “C:\PHP\extensions;”

I have simply used all the default locations provided by the install programs. The next thing you need to edit applies to the Dynamic Extensions section, there look through the commented out list of extensions for extension=php_java.dll and uncomment it.

These changes need to take effect and this can only be done by restarting your web server. If the server started up alright and PHP is still running then you edited properly the ini file, which by no means indicates that the configuration was successful. Only one way to find out and that’s to try it out. Here is a bit of code I found on the PHP site (http://www.php.net/) that helped me test my installation. The full code can be found here: http://www.php.net/manual/en/ref.java.php

<?php
// get instance of Java class java.lang.System in PHP
$system = new Java(‘java.lang.System’);

// demonstrate property access
print ‘Java version=’.$system->getProperty(‘java.version’).’ <br>’;
print ‘Java vendor=’ .$system->getProperty(‘java.vendor’).’ <br>’; print ‘OS=’.$system->getProperty(‘os.name’).’ ‘.
$system->getProperty(‘os.version’).’ on ‘.
$system->getProperty(‘os.arch’).’ <br>’;

// java.util.Date example
$formatter = new Java(‘java.text.SimpleDateFormat’,
“EEEE, MMMM dd, yyyy ‘at’ h:mm:ss a zzzz”);

print $formatter->format(new Java(‘java.util.Date’));
?>

This sample PHP code access the Java System class, accesses a few properties and returns the appropriate values. If everything is configured properly then you should see something like this:

Java version=1.4.0_01
Java vendor=Sun Microsystems Inc.
OS=Windows XP 5.1 on x86
Tuesday, October 22, 2002 at 11:56:32 AM Greenwich Mean Time

If you are having any difficulties just make sure you are pointing to the right locations for the java environment. Another suggestion I can make is to take a closer look at your php.ini file and look for:

; Directory in which the loadable extensions (modules) reside.
extension_dir = “C:\PHP\extensions”

Just make sure that extension_dir is pointing at the right location for your PHP extensions. So far so good, next time I’ll talk a little about my first forays into running my own classes (and that’s where the tribulations truly begin).