Working with Java Bridge

In this Topic Hide


Testing the Bridge Connection

Before using the Java Bridge API


The Java Bridge is only active when the Java Bridge component is installed and activated (see the Installation Guide). The component's status and settings can be viewed and configured in the Administration Interface, from Server Setup | Components.


The Java Bridge requires that you have Sun Microsystems JRE 1.4 (or later) or IBM Java 1.4.2 (or later) installed on your computer. During or after installing (depending on the installation type), you are prompted to direct the installer to the JRE location. Therefore, you should already have JRE installed. 64-bit JRE is not supported.
More information about JREs and the latest updates can be obtained from the SUN Microsystems Website.


This procedure describes how to configure the target Java runtime environment.




Configuring the runtime environment:

Use the following command to run JavaMW:

java com.zend.javamw.JavaServer

For correct execution, the classpath should include the javamw.jar file in the directory where JavaMW is installed.



Usage Example


UNIX, Linux, IBM i and Mac <install_dir>/bin/javamw.jar

Windows <install_dir>\bin\javamw.jar


Testing the Bridge Connection

The following code sample shows how you can, as an initial step, test the connection between your PHP and Java environments to ensure that the Java Bridge is defined properly and communicates with the correct Java. This code demonstrates the interaction between a PHP application and Java objects that occurs in the Java Bridge implementation.



Instructions on how to complete a procedure

To test the Java Bridge connection:

Create a new PHP script to create a Java object, as in the example below:


// create Java object

  $formatter = new Java("java.text.SimpleDateFormat",

                        "EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz");

  // Print date through the object

  print $formatter->format(new Java("java.util.Date"))."\n";

  // You can also access Java system classes

  $system = new Java("java.lang.System");

  print $system."\n"; // will use toString in PHP5

  print "Java version=".$system->getProperty("java.version")." <br>\n";

  print "Java vendor=".$system->getProperty("java.vendor")." <p>\n\n";

  print "OS=".$system->getProperty("")." ".

              $system->getProperty("os.version")." on ".

              $system->getProperty("os.arch")." <br>\n"; ?>

If the Java Bridge is correctly installed and running, you should receive the following response:

Friday, June 13, 2008 at 8:45:57 PM U.S Daylight Time class java.lang.System Java version=1.6.0_06 Java vendor=Sun Microsystems Inc.

OS=Linux on i386

This output shows the date, Java version, vendor and operating system and indicates that the connection is complete.
If you receive an error message instead of the expected output information, one of the following problems may have occurred:

  1. The Java Bridge is not installed

  2. The Java Bridge extension is not running (Server Setup | Components)

  3. The Java Bridge Server needs to be restarted (Server Setup | Components)

  4. The requested .jar file does not appear in the environment's classpath.

Once the connection is established, you can start using the API to call Java objects from your PHP.

Before using the Java Bridge API

Before you start incorporating the Java Bridge API in your code, you must be aware that when you call Java from PHP, you must use Java coding standards to call the correct objects, because the Java Bridge does not perform dynamic data conversion. You must perform the type conversion in your PHP code.
For example,

Usage Example


If you call a Java method that looks like this:

public void doSomething(int i);

Using what you would expect to work in PHP:

$var = "1"


The Java Bridge throws an exception. To avoid this, use the following line of code to convert the parameter from a string to a numeric value before the Java Bridge passes it:

$javaObject->doSomething($var + 0);


For more information, see the API, or Java Bridge Use Cases.



Related Links

Related Links:


Java Bridge Use Cases

Zend Java Bridge - Configuration Directives

Zend Java Bridge - PHP API