首页 > 开发 > PHP > 正文

在PHP中调用JAVA

2024-05-04 23:04:23
字体:
来源:转载
供稿:网友
   安装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.



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表