Sie sind auf Seite 1von 2

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.

To test the Java Bridge connection:


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

<?php
// 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("os.name")." ".
$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 2.6.25.3-18.fc9.i686 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 (Configurations | Components)
3. The Java Bridge Server needs to be restarted (Configurations |
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.