PHP 5, PHP/Java Bridge & Apache on XP PRO + SP2 and examples

Posted : Tue 18/07/2006
Updated: Thu 16/04/2007

Tested Environment

*Add-in modules for Apache 1.3x or 2.0.x are not compatible with Apache 2.2.x. If you are running PHP 5.x, you must use the Apache version 2.0.x.

Installed Directories

Java

  1. Make sure you set Environment Variables for Java (CLASSPATH and PATH). It looks like;
    CLASSPATH = .
    JAVA_HOME = C:\Program Files\Java\jdk1.5.0_10
    PATH = %PATH%;%JAVA_HOME%\bin
    

    JAVA CLASSPATH

Apache HTTP Server

  1. In general, it's simple to install. I installed the Apache HTTP Server for the localhost (127.0.0.1) on Port 80 (default).

    Apache HTTP Server 1

  2. To allow the Apache HTTP Server to by pass Windows Firewall, click Unblock.

    Apache HTTP Server & Firewall

  3. To install PHP as an Apache module, add these lines to your Apache2 httpd.conf file (C:\Program Files\Apache Group\conf):

    # For PHP 5 do something like this:
    LoadModule php5_module "c:/php/php5apache2.dll"
    AddType application/x-httpd-php .php
    
    # For syntax highlighted .phps files, also add
    AddType application/x-httpd-php-source .phps
    
    # configure the path to php.ini
    PHPIniDir "C:/php"
    
    Apache HTTP Server Config

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. 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:\Program Files\Apache Group\Apache2\htdocs\test.
  3. Open http://localhost/test/phpinfo.php in your web browser.

    phpinfo() 1

    phpinfo() 2

  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') . '<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'));
    
    ?>
    
    

    phpinfo() 2

  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 CLASS

  8. Copy the HelloWorld.jar file into the C:\Program Files\Apache Group\Apache2\htdocs\test directory.

    Java CLASS 1

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

    Java CLASS 2

More Examples

  1. Example - Config.java & config.php

    C:\Program Files\Apache Group\Apache2\htdocs\Config.java.

    public class Config
    {
    	private String dbHost = "myhost";
    	private String dbUser = "myuser";
    	private String dbPass = "mypass";
    	private String dbName = "mydb";
    
    	// constructor
    	public Config()
    	{}
    
    	// constructor
    	public Config(String host, String user, String pass, String name)
    	{
    		dbHost = host;
    		dbUser = user;
    		dbPass = pass;
    		dbName = name;
    	}
    
    	public String getDbHost()
    	{
    		return dbHost;
    	}
    
    	public String getDbUser()
    	{
    		return dbUser;
    	}
    
    	public String getDbPass()
    	{
    		return dbPass;
    	}
    
    	public String getDbName()
    	{
    		return dbName;
    	}
    }
    

    C:\temp\javac Config.java
    C:\temp\jar cvf Config.jar Config.class
    C:\Program Files\Apache Group\Apache2\htdocs\test\Config.jar

    C:\Program Files\Apache Group\Apache2\htdocs\test\config.php.

    <?
    
    java_require('http://localhost/test/Config.jar');
    
    $myObj = new Java("Config");
    
    echo (string) $myObj->getDbHost() . '<br/>';
    echo (string) $myObj->getDbUser() . '<br/>';
    echo (string) $myObj->getDbPass() . '<br/>';
    echo (string) $myObj->getDbName();
    
    ?>
    

    Config.java

  2. Example - Config1.java & config1.php

    C:\Program Files\Apache Group\Apache2\htdocs\test\Config1.java.

    public class Config1
    {
    	private String dbHost = ""; 
    	private String dbUser = ""; 
    	private String dbPass = ""; 
    	private String dbName = "";
    
    	// constructor
    	public Config1()
    	{}
    
    	// constructor
    	Config1(String host, String user, String pass, String name)
    	{
    		dbHost = host;
    		dbUser = user;
    		dbPass = pass;
    		dbName = name;
    	}
    
    	public String getDbHost()
    	{
    		return dbHost;
    	}
    
    	public void setDbHost(String host)
    	{
    		dbHost = host;
    	}
    
    	public String getDbUser()
    	{
    		return dbUser;
    	}
    
    	public void setDbUser(String user)
    	{
    		dbUser = user;
    	}
    
    	public String getDbPass()
    	{
    		return dbPass;
    	}
    
    	public void setDbPass(String pass)
    	{
    		dbPass = pass;
    	}
    
    	public String getDbName()
    	{
    		return dbName;
    	}
    
    	public void setDbName(String name)
    	{
    		dbName = name;
    	}
    }
    
    

    C:\Program Files\Apache Group\Apache2\htdocs\test\config1.php.

    <?php
    
    // config1.php
    
    java_require('http://localhost/test/Config1.jar');
    
    $myObj = new Java("Config1");
    
    $myObj->setDbHost("localhost");
    $myObj->setDbUser("sk33");
    $myObj->setDbPass("abcd");
    $myObj->setDbName("test");
    
    echo (string) $myObj->getDbHost() . '<br/>';
    echo (string) $myObj->getDbUser() . '<br/>';
    echo (string) $myObj->getDbPass() . '<br/>';
    echo (string) $myObj->getDbName();
    
    ?>
    

    Config1.java

  3. Example - SalesTax.java, salestax.php, salestax.htm

    C:\Program Files\Apache Group\Apache2\htdocs\test\SalesTax.java.

    import java.util.*;
    import java.text.*;
    
    public class SalesTax
    {
    
        private double tax = 0.0;
        private double price = 0.0;
        private double salesTax = 0.0;
    
        public SalesTax()
        {}
    
        public String SalesTax(double price, double salesTax)
        {
            tax = price * salesTax;
    
            NumberFormat nf = null;
    
            nf = NumberFormat.getCurrencyInstance();
    
            String priceOut = nf.format(price);
            String taxOut = nf.format(tax);
    
            nf = NumberFormat.getPercentInstance();
            String salesTaxOut = nf.format(salesTax);
    
            String str = "Sales Tax of " + salesTaxOut +
                " on " + priceOut + " equals " + taxOut + ".";
    
            return str;
        }
    }
    

    C:\Program Files\Apache Group\Apache2\htdocs\test\salestax.php

    <?php
    
    if (isset($_POST["submit"]))
    {
    	java_require('http://localhost/test/SalesTax.jar');
    
    	$salesTax = new Java("SalesTax");
    
    	$price = (double) $_POST["price"];
    	$tax = (double) $_POST["tax"];
    
    	echo (String) $salesTax->SalesTax($price, $tax);
    }
    
    ?>
    

    C:\Program Files\Apache Group\Apache2\htdocs\test\salestax.htm.

    <html>
    <head>
    <title></title>
    </head>
    <body>
    
    <form action="salestax.php" method="post">
    Price: ($)<br/>
    <input type="text" name="price" size="15" maxlength="15"><br/>
    Tax Rate: (0.1 for 10%)<br/>
    <input type="text" name="tax" size="15" maxlength="15"><br/>
    <input type="submit" name="submit" value="Calculate!">
    </form>
    
    </body>
    </html>
    

    SalesTax.java 1

    SalesTax.java 2

Sample Files

References

  1. PHP - install.txt
  2. PHP/Java Bridge - INSTALL.WINDOWS & INSTALL.J2EE
  3. PHP4 - Java integration on Win32 and examples

Copyright © 2001-2007. Kang, S. Information Systems. University of Wollongong. Australia. All rights reserved.