在PHP中调用JAVA

   安装PHP4.3.5RC3.dev(Win32)+Apache 2.0.52(For Win32)和j2sdk 1.4 (For Win32)都不用说了,如果不会安装,请看相关手册。
    在PHP配置文件php.ini中找到 ”  ;extension=php_java.dll   ”  这上行,并把前面的分号去掉;
    再找到:
             [Java]
              ;java.class.path = 
              ;java.home = 
              ;java.library = 
              ;java.library.path =

    改为:
              java.class.path = “JAVA的CLASS_PATH;(将要放置自己的class文件的目录)d:\java_for_php”
              java.home = “JAVA的JAVAHOME”
              java.library = “JAVA的JAVAHOME\jre\bin\server\jvm.dll”
              java.library.path = “PHP安装目录\extensions\”

    修改好后,php.ini 文件必须放在Windows安装目录内,重新启动Apache。

    在d:\java_for_php内新建一个PhpCallJava.java ,内容如下:
    public class PhpCallJava {
            public String testString;
           
            public String test (String str) {
                     if (str.equals(“”)) {
                         return “This is a empty string!”;
                     } else {
                         return str;
                     }
            }

             public String GetTestString () {
                      return “Your string is: ” + testString;
             }
    }
   编译PhpCallJava.java 生成PhpCallJava.class

    新建一个PHP文件,内容如下:
    <?php

           $myjava = new Java(“PhpCallJava”);
           echo “Test Result are <b>”.$myjava->test(“Hello JAVA, I m PHP.”).”</b><br>\n”;
 
           $myjava->testString = “JAVA, I gave you a string.”;
           echo “You have set string is: <b>”.$myjava->testString.”</b><br>\n”;
           echo “My java method reports:<b>”.$myjava->GetTestString ().”</b><br>\n”;
    ?>

    结果:
          Test Result are Hello JAVA, I m PHP.
          You have set string is: JAVA, I gave you a string.
          My java method reports:Your string is: JAVA, I gave you a string.