首页 > 应用开发 > IT综合 > PHP & Java(1) (转)

PHP & Java(1) (转)

原创 IT综合 作者:worldblog 时间:2007-12-04 08:26:46 0 删除 编辑
PHP & Java(1) (转)[@more@]">Mark Nold
Joost Soeterbroek

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"> plus the">4.0.3 Zend Optimiser and jdk 1.2.2 from j2se/?frontpage-javaplatform"> 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.x.xbin directory is in your path. This will make compiling your Java Classes easier. On WIN9X add


to your autoexec.bat. On NT add 


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 these items must be set correctly in your php.ini file for the Java extension to work. 

Step 2: Modifying your php.ini. You need to add something similiar to your php.ini file

[java] extension=php_java.dll java.library.path=c:webphp4extensionsjava.class.path="c:webphp4extensionsjdk1.2.2php_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 cannot 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:

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

nn"; print "OS=".$system->getProperty("")." ". $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 examples. If you have correctly installed everything, you should see some 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

A very simple example, but it shows you can access currently available Java classes. Once you have this example working, you have successfully set up the PHP Java extension.

来自 “ ITPUB博客 ” ,链接:,如需转载,请注明出处,否则将追究法律责任。

下一篇: PHP & Java(4) (转)
请登录后发表评论 登录
  • 博文量
  • 访问量