home blogs resources search downloadsregistermembers: 7132sign-in
PHP Flash Java Ruby Windows Linux
Hiveminds's picture

Hiveminds | Wed, 2008-07-09 19:32  tags: , ,

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

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

  1. Under 'Home Directory', change the Execute Permissions to 'Scripts only'

    pjb_iis-1.png

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

    pjb_iis-2.png

  3. Stop IIS completely.
  4. Start IIS again.

PHP 5 & Java Bridge

  1. You may use some sort of tools to unzip the php-java-bridge_3.2.1_j2ee.zip
    file. I used ALZip.

  2. Copy JavaBridge.jar from the JavaBridge.war to C:\PHP\ext.

    JavaBridge.jar

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

    java-x86-windows.dll

  4. Rename java-x86-windows.dll (from C:\PHP\ext) to php_java.dll

    rename java-x86-windows.dll

  5. To set up a valid configuration file for PHP, make a copy of
    php.ini-recommended then rename it to the php.ini.

    PHP INI

  6. Set doc_root to "c:\Inetpub\wwwroot".
  7. 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
...

PHP exe 1

PHP exe 2

PHP exe 3

Test & Examples

  1. Open Notepad, then type (phpinfo.php)

    <?php

    phpinfo
    (); 

    ?>

  2. Save it as phpinfo.php into c:\Inetpub\wwwroot.
  3. Open http://localhost/phpinfo.php in your web browser.

    phpinfo() 1

    phpinfo() 2

    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.

  4. 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') . '&lt;br/&gt;';
    echo 
    'Java vendor=' $system->getProperty('java.vendor') . '&lt;br/&gt;';
    echo 
    'OS=' $system->getProperty('os.name') . ' ' .
                 
    $system->getProperty('os.version') . ' on ' .
                 
    $system->getProperty('os.arch') . ' &lt;br/&gt;';
    // 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'));

    ?>

    java1.php

  5. Example - HelloWorld.java


    public class HelloWorld {
        String hw = "Hello World";
        public String getHelloWorld() {
            return hw;
        }
    }

  6. To compile a java source file, type "javac HelloWorld.java"
  7. To create a JAR file from the class file, type "jar cvf HelloWorld.jar
    HelloWorld.class".

    Java 1

  8. Copy the HelloWorld.jar file to C:\Inetpub\wwwroot.

    Java 2

  9. 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();

    ?>

  10. Open http://localhost/helloworld.php in your Web browser.

    Java 3

References

  1. PHP - install.txt
  2. 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
a Visitor posted on: Fri, 2008-08-08 06:40.

I have follwed all the steps specified and also i copied the files to c:\windows but the java module does not shown and when i tried to exeute java1.php i got a page can not be displayed page with HTTP 500 internal error. How to execute the java file from php. please tell me.

java posted on: Fri, 2008-08-08 09:51.

It sounds as though you have not set up PHP to run on IIS. You'll have set up PHP to run as a ISAPI module or CGI exe.

Post new comment

required
How to use html
 
 
Silverlight Silverlight Content Management Systems Joomla! ASP.NET Adobe Flex Wordpress

Editor's choice

Some of the better articles, stories and tutorials found at Hiveminds.

Find more

Find more of Hiveminds articles, stories, tutorials and user comments by searching.




Picked links

Hand picked websites and articles from around the web that provide quality reading.