PHP DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our LAMP news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Print Subscribe to PHP Subscribe to Newsletters

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




Recommended for You

Tagged Articles

Post to del.icio.us

This article has been tagged:

programming

Articles that share the tag programming:

Rolling with Ruby on Rails (1374 tags)

Very Dynamic Web Interfaces (279 tags)

Ajax on Rails (231 tags)

Understanding MVC in PHP (202 tags)

A Simpler Ajax Path (186 tags)

View All

java

Articles that share the tag java:

Profiling Your Applications with Eclipse Callisto (113 tags)

What Is a Portlet (101 tags)

Parsing an XML Document with XPath (79 tags)

The REST of the Web (75 tags)

Eclipse Plugins Exposed, Part 1: A First Glimpse (69 tags)

View All

php

Articles that share the tag php:

Understanding MVC in PHP (477 tags)

The PHP Scalability Myth (123 tags)

The Dynamic Duo of PEAR::DB and Smarty (53 tags)

PHP Form Handling (43 tags)

Very Dynamic Web Interfaces (39 tags)

View All

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

Sign up today to receive special discounts,
product alerts, and news from O'Reilly.
Privacy Policy >
View Sample Newsletter >
  • Youtube
  • http://www.youtube.com/OreillyMedia
  • Twitter
  • Subscribe
  • View All RSS Feeds >
O'Reilly Media

800-889-8969 or 707-827-7019
Monday-Friday 7:30am-5pm PT
©2011, O'Reilly Media, Inc.
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
  • About O'Reilly
  • Academic Solutions
  • Contacts
  • Customer Service
  • Careers
  • Press Room
  • Privacy Policy
  • Terms of Service
  • Writing for O'Reilly
  • Community
  • Authors
  • Forums
  • Membership
  • Newsletters
  • RSS Feeds
  • User Groups
  • More O'Reilly Sites
  • igniteshow.com
  • makerfaire.com
  • makezine.com
  • craftzine.com
  • labs.oreilly.com
  • Partner Sites
  • PayPal Developer Zone
  • O'Reilly Insights on Forbes.com