PHP中呼叫JAVA,

hnbk8發表於2007-03-30
安裝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.

 

相關文章