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.
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