PHP DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


PHP and Java

by W.J. Gilmore
06/14/2001

Do PHP developers ever sleep? I'm starting to wonder what these caffeine-entranced insomniacs are going to think of next. I'm asking this question because I can't seem to understand how PHP always seems to have just the right extension to accomplish the task at-hand. Mathematical functions? No problem. Databasing? Simple. Butter my toast? Well, we're not that far yet, but give some PHP programmer a spare weekend locked away in his office, and some script will soon be asking me "Is margarine okay?". You just wait and see.

Why am I ranting about this? It's because I've recently begun messing around with PHP 4.0's Java extension, and I'm in awe over the whole new dimension that using Java classes within PHP code offers. As if it's not enough that I'm using my favorite scripting language, I can also use my other favorite language simultaneously? What more could a geek like me ask for?

Okay, enough babbling. In this article, I'm going to introduce this great new extension in detail, providing not only information regarding the necessary steps to configure Java support for PHP, but also highlighting several examples that are geared toward showing you just what is possible when intertwining these two languages.

Configuration

Java support is not native to the PHP 4.0 distribution; therefore, you'll need to perform a few installation and configuration steps in order to use Java and PHP together. However, these steps are rather trivial and should only take a few minutes to accomplish.

JDK installation

First off, you'll want to install the JDK. For purposes of this article, I used JDK 1.2.2, and will refer to the JDK throughout this article using this version title. However, you are free to use any version you choose (1.1.8, 1.2, or 1.3). You can find the JDK at http://java.sun.com. Simply follow the directions that are provided with the respective platform installation, and you will have this up and running in no time. Once you've installed the JDK, move on to the following sections.

Non-Windows users

You'll need to reconfigure PHP using the following option:

./configure -with-java [-other-configuration-options]

If you are unfamiliar with how PHP is configured and installed on a non-Windows system, I would suggest checking out Darrell Brogdon's excellent article "Basic Installation of PHP on a Unix System".

Windows users

After installing the JDK, I would highly recommend adding the JDK path to your environment path. This is accomplished by opening autoexec.bat and adding the line:

PATH=%PATH%;C\jdk1.2.2\bin

Downloading the Win32 Java extensions

If you're a Windows user, you'll also need PHP's Win32 Java extensions (available for the 1.1.8, 1.2.2, and 1.3 versions). Be sure to download the one that matches the JDK version you installed. These extensions are packaged as a Zip file. Once downloaded, unzip the file, and extract it to a folder named jdkx.x.x (the x's denoting your version number) within the folder where PHP is installed on your machine. Two of these files, namely jawt.php and jver.php are just test files, so you can ignore them for the moment. The file php_java.dll should be placed within the extensions folder, and the php_java.jar can be left right in the jdk.x.x.x folder.

Modifying the php.ini file

After you've installed the JDK, the next step is to modify the php.ini file. The first action you'll need to take is to add the following line to the extensions section:

extension=php_java.dll

Next, scroll down to the section titled [Java]. You'll see four configuration directives, namely java.class.path, java.home, java.library, and java.library.path. Modify these directives to read as seen below. I'll assume that you've installed PHP in the directory C:\php4. If you haven't please change the following to point to the correct directory path as necessary.

; This must point to the location of php_java.dll.
java.library.path = "C:\php4\extensions;"

; This points to the directory where your Java 
; classes will be stored.  You can designate multiple
; paths, each path separated by a semicolon.
; It must also include the location of php_java.jar
java.class.path = "C:\php4\extensions\jdk1.2.2\php_java.jar;"

; This points to the bin directory of the JDK.
java.home = "C:\jdk1.2.2\bin"

; This must point to the Java Virtual Machine (jvm.dll) file.
java.library = "C:\jdk1.2.2\jre\bin\classic\jvm.dll"
Once you've successfully installed the JDK and configured the PHP php.ini file, take a moment to test your configuration using the example shown in the next section, "Using Java's Predefined Library."

Using Java's predefined library

Once configured, you can begin using Java's predefined class libraries. In Listing 1, I make use of the java.lang.system library to display the total number of seconds since January 1, 1970.

Listing 1: Using Java's predefined java.lang.System library.

<?php

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

print "Total seconds since January 1, 1970: 
".$systemInfo->currentTimeMillis();

?>

Since this Java class has already been compiled, there is no need for any other steps other than to instantiate the class from within the Java() command. Executing Listing 1, you should see output similar to the following:

Total seconds since January 1, 1970: 978463526830

While the precompiled libraries (Java calls them "packages") are certainly useful, chances are you will want to use custom libraries written by both other developers or yourself. I'll show you how this is accomplished in the next section, "Using Custom Libraries."

The following is an error that commonly arises when making the first attempts to integrate PHP and Java:

Warning: java.lang.ClassNotFoundException

There are three likely errors that could be the cause of this message:

  • The specified .class file is not located on the java.class.path as specified in the php.ini configuration file.
  • You misspelled the .class file
  • The .class file is not in the correct directory as specified by the instantiation path

Therefore, if you are receiving this error, take a moment to review each of the above possibilities as they pertain to your code.

Pages: 1, 2

Next Pagearrow




Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!


Sponsored by: