PHP & Java(1)

原创 2001年01月09日 15:27:00

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 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.x/bin 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


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:


  $system = new Java("java.lang.System");
  print "Java version=".$system->getProperty("java.version")." <br>/n";
  print "Java vendor=".$system->getProperty("java.vendor")." <p>/n/n";
  print "OS=".$system->getProperty("")." ".
              $system->getProperty("os.version")." on ".
              $system->getProperty("os.arch")." <br>/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.

php调用java webservice接口

1、string(74) "phpType is struct, but value is not an array: see debug output for details"   解决办法:对于要...
  • liuhongwei_study
  • liuhongwei_study
  • 2017-06-14 16:23:19
  • 745


  • qq_34675830
  • qq_34675830
  • 2017-05-30 14:56:10
  • 349

php exec java -jar 不能执行命令

其实问题很简单,只要加java全路径就可以了。 例如:exec("java -jar /home/florinbuda/NetBeansProjects/monkey1/dist/monkey1.j...
  • u010308173
  • u010308173
  • 2017-03-20 10:56:16
  • 252

PHP gzcompress 压缩,java实现gzuncompress

PHP gzcompress压缩,java实现gzuncompress
  • havedream_one
  • havedream_one
  • 2017-03-05 09:17:54
  • 1189

exec 函数执行错误——应用:PHP提取apk的icon图标图片

作者:林子木 wolinxuebin 转载请保留:     今天在做项目,从apk中读取图片icon信息。因为有大量的数...
  • wolinxuebin
  • wolinxuebin
  • 2012-07-30 19:41:51
  • 2465


public static void main(String[] args) {     try {       System.out.println(getMd5("123".getBytes(...
  • u012108514
  • u012108514
  • 2017-09-07 11:01:01
  • 158

php md5(chr())对应java中的实现

php 中 chr() 函数   从指定的 ASCII 值返回字符。,由于其中chr函数是按照ASCII编码返回字符的! 但是java中字符使用unicode编码表示的所以直接和java中的char...
  • yangchangji
  • yangchangji
  • 2017-02-07 15:48:35
  • 1597


  • Handsome_fan
  • Handsome_fan
  • 2016-11-19 20:00:51
  • 1528

css php java 手册

  • 2011年05月11日 22:25
  • 7.39MB
  • 下载


        Java中增加了一种“无符号”右移操作符(>>>),它使用“零扩展”:无论正负,都在高位插入0。这一操作符是C或C++中所没有的!        整数-1右移10位后怎么变成41943...
  • navygong
  • navygong
  • 2006-09-05 10:38:00
  • 1295
您举报文章:PHP & Java(1)