Mark Noldjoost Soterbroek
The Java extension is an extremely exciting tool. By learning how to use this module, you can extend PHP by the power of all available Java classes. To show you the basics of the Java extension, this article will cover installation and a few code examples OF USING PHP AND JAVA TOGETHER.
Windows Installation
The following configuration has been tested with Apache 1.3.12, PHP 4.0.3 binaries from www.php4win.de plus the 4.0.3 Zend Optimiser and JDK 1.2.2 from java.sun.com. We have also tested this configuration with older Versions of the JDK and The Various MS Webservers (PWS AND IIS) on Windows 95, Windows 98 and NT4.
Step 1: Install the JDK This is fairly simple, as the JDK installs without many questions It might be useful to check your environment (autoexec.bat in Windows 9x and System under Control Panel in NT) and make sure the jdk1.xx.. / bin directory is in your path. this will make match your java classes easier. on Win9X Add
PATH =% path%; c: /jdk1.2.2/bin
To your autoexec.bat. on NT Add
; C: /jdk1.2.2/bin
To the end of the path environment variable. it is also important to note in your autoexec.bat file, The PHP Java Extension
Ignores The Java_Home and ClassPath Set Up in The Environment. This IS IMPORTANT BECAUSE The Items Must Be Set Correctly in Your php.ini file for the java extension to work.
Step 2: modify your php.ini. You need to add something similiar to your php.ini file
[java]
EXTENSION = php_java.dll
Java.library.path = c: /web/php4/extensionsjava.class.path= "c: /web/php4/extensions/jdk1.2.2/php_java.jar; c: / myclasses"
Typically, people put the extension = php_java.dll directive with the rest of the extensions, but it can sit happily under [java]. The java.library.path must be set in the location of the php_java.dll, and java.class . .path must include the location of php_java.jar The java.class.path should also include the path to other classes you may wish to use (note the double quotes!) In these examples, we will be talking about c:. / . myclasses you should also note that the single period is ignored by PHP and Java As far as we know, you can not set PHP to look in the current directory for its Java classes.Step 3:.. Testing your Install Now, you're Ready to Go. Create a PHP File That Looks Something Like this:
PHP
$ system = new java ("java.lang.system");
Print "Java Version =". $ SYSTEM-> GetProperty ("java.version"). "
/ n";
Print "java vendor =". $ system-> getproperty ("java.vendor"). "
/ n / n";
Print "OS =". $ system-> getproperty ("os.name"). "" ".
$ system-> getproperty ("os.version"). "on".
$ system-> getproperty ("os.arch"). "
/ n";
$ FORMATTER = New Java ("java.text.simpledateformat", "Eeee,
MMMM DD, YYYY 'AT' H: MM: SS A ZZZZ ");
Print $ Formatter-> Format (New Java ("Java.util.date")). "/ n";
?>
NB: this is taken directly from sam ruby's example. If you have, you shouth, you have results like:
Java Version = 1.2.2
Java vendor = Sun Microsystems Inc.
OS = Windows 95 4.10 on x86
Wednesday, October 18, 2000 At 10:22:45 Am China Standard Time