
In a previous tutorial it was shown how to set up the php/java bridge on Windows using the Apache
webserever. In this tutorial you learn how to do the same using Windows IIS web server. Though this is a
beginners tutorial you are expected to have some familiarity with IIS. Notice that the version of the PHPJava bridge
used is the older version 3.2.1. Though not the latest version this is the most Windows friendly software. It contains a dll
file that surpasses the one distributed in PHP 5. There is also documentation to be found and you can ask questions about usage on the
mailing list.
Hopefully Zend will take it upon itself to work more closely with Sun and finish the native PHP java bridge. It is a very necessary
tool.
Tested Environment
Installation Directories
- C:\PHP
- C:\Inetpub
- C:\Program Files\Java\jdk1.5.0_11
Java
- Set Environment Variables for Java (CLASSPATH, JAVA_HOME and PATH).
CLASSPATH = . JAVA_HOME = C:\Program Files\Java\jdk1.5.0_11 PATH = %PATH%;%JAVA_HOME%\bin
IIS
To use the ISAPI module, do the following:
- Under 'Home Directory', change the Execute Permissions to 'Scripts only'

- Click on the 'Configuration' button, and choose the Application Extension
Mapping tab. Click Add and set the Executable path to C:\php\php5isapi.dll.
Supply .php as the extension. Leave 'Method exclusions' blank, and check the 'Script engine' checkbox. Now, click OK a few times.

- Stop IIS completely.
- Start IIS again.
PHP 5 & Java Bridge
- You may use some sort of tools to unzip the php-java-bridge_3.2.1_j2ee.zip
file. I used ALZip.
- Copy JavaBridge.jar from the JavaBridge.war to C:\PHP\ext.

- Copy java-x86-windows.dll from the JavaBridge.war to C:\PHP\ext.

- Rename java-x86-windows.dll (from C:\PHP\ext) to php_java.dll
- To set up a valid configuration file for PHP, make a copy of
php.ini-recommended then rename it to the php.ini.

- Set doc_root to "c:\Inetpub\wwwroot".
- Edit the php.ini so that the
"extension_dir" points to your PHP 5 extension directory.
... extension_dir = C:\php\ext extension = php_java.dll ...



Test & Examples
- Open Notepad, then type (phpinfo.php)
<?php
phpinfo();
?>
- Save it as phpinfo.php into c:\Inetpub\wwwroot.
- Open http://localhost/phpinfo.php in your web browser.
Note: If your IIS does not load the Java module automatically, most cases, you need to move php.ini (from C:\php) and php_java.dll (from c:\php\ext) to C:\Windows. None of methods listed on the PHP install.txt worked such as setting a path to PHP or editing registry for PHP.
- Example - java1.php
<?php
// get instance of Java class java.lang.System in PHP $system = new Java('java.lang.System'); // demonstrate property access echo 'Java version=' . $system->getProperty('java.version') . '<br/>'; echo 'Java vendor=' . $system->getProperty('java.vendor') . '<br/>'; echo '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"); echo $formatter->format(new Java('java.util.Date'));
?>
- Example - HelloWorld.java
public class HelloWorld { String hw = "Hello World"; public String getHelloWorld() { return hw; } }
- To compile a java source file, type "javac HelloWorld.java"
- To create a JAR file from the class file, type "jar cvf HelloWorld.jar
HelloWorld.class".
- Copy the HelloWorld.jar file to C:\Inetpub\wwwroot.

- Code the following, and save it as helloworld.php in C:\Inetpub\wwwroot.
<?php
java_require('http://localhost/HelloWorld.jar'); $myObj = new Java('HelloWorld'); // display Hello World echo (String) $myObj->getHelloWorld();
?>
- Open http://localhost/helloworld.php in your Web browser.

References
- PHP - install.txt
- PHP/Java Bridge - INSTALL.WINDOWS & INSTALL.J2EE
Originally published by: S.Kang
This article brought to you by the
Hiveminds Magazine - Staff. Contact us if you want to post an article or announcement anonymously
|
|