PHP怎么调用JAVA 类库

分类: php基本   出处:iocblog整理  更新时间:2009-12-04   添加到收藏  


  java是个非常强大的编程利器,它的扩展库也是非常的有用,这篇教程,主要讲述怎样使用php调用功能强大的java 类库(classes)。为了方便你的学习,这篇教程将包括java的安装及一些基本的例子。
  
    windows下的安装
  
    第一步:安装jdk,这是非常容易的,你只需一路回车的安装好。然后做好以下步骤。
  
     在 win9x 下加入 :“path=%path%;c:jdk1.2.2in” 到autoexec.bat文件中
  
     在 nt 下加入 “;c:jdk1.2.2in”到环境变量中。
  
    这一步是非常重要的,这样php才能正确的找到需调用的java类。
  
    第二步:修改你的php.ini文件。
  
  [java]
  extension=php_java.dll
  java.library.path=c:webphp4extensions
  java.class.path="c:webphp4extensionsjdk1.2.2php_java.jar;c:myclasses"
  
    在php.ini中加入extension=php_java.dll,并在[java]中,设定好java.class.path,让它指向php_java.jar,如果你使用新的java类,你也应该存入这个路径,在这篇例子中,我们使用c:myclasses这个目录。
  
    第三步:测试环境,创建如下php文件:
   
  <?php
  
  $system = new java("java.lang.system");
  print "java version=".$system->getproperty("java.version")." <br> ";
  print "java vendor=".$system->getproperty("java.vendor")." <p> ";
  print "os=".$system->getproperty("os.name")." ".
  $system->getproperty("os.version")." on ".
  $system->getproperty("os.arch")." <br> ";
  
  $formatter = new java("java.text.simpledateformat","eeee,
  mmmm dd, yyyy 'at' h:mm:ss a zzzz");
  print $formatter->format(new java("java.util.date"))." ";
  
  ?>
  
    如果你正确安装了,你将会看到以下信息:
  
  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
  
    这样,我们就已经成功的建立起了可以使用java类的php运行环境,我们可以开始我们接下去的课程了。
  
    例子1:创建和使用你自己的java类
  
    创建你自己的java类非常容易。新建一个phptest.java文件,将它放置在你的java.class.path目录下,文件内容如下:
  
  public class phptest{
  /**
  * a sample of a class that can work with php
  * nb: the whole class must be public to work,
  * and of course the methods you wish to call
  * directly.
  *
  * also note that from php the main method
  * will not be called
  */
  
  public string foo;
  
  /**
  * takes a string and returns the result
  * or a msg saying your string was empty
  */
  public string test(string str) {
  if(str.equals("")) {
  str = "your string was empty. ";
  }
  return str;
  }
  
  /**
  * whatisfoo() simply returns the value of the variable foo.
  */
  public string whatisfoo() {
  return "foo is " + foo;
  }
  
  
  /**
  * this is called if phptest is run from the command line with
  * something like
  * java phptest
  * or
  * java phptest hello there
  */
  public static void main(string args[]) {
  phptest p = new phptest();
  
  if(args.length == 0) {
  string arg = "";
  system.out.println(p.test(arg));
  }else{
  for (int i=0; i < args.length; i++) {
  string arg = args[i];
  system.out.println(p.test(arg));
  }
  }
  }
  }
  
    创建这个文件后,我们要编译好这个文件,在dos命令行使用javac phptest.java这个命令。
  
    为了使用php测试这个java类,我们创建一个phptest.php文件,内容如下:
  
  <?php
  
  $myj = new java("phptest");
  echo "test results are <b>" . $myj->test("hello world") . "</b>";
  
  $myj->foo = "a string value";
  echo "you have set foo to <b>" . $myj->foo . "</b><br>n";
  echo "my java method reports: <b>" . $myj->whatisfoo() . "</b><br>n";
  
  ?>
  
    如果你得到这样的警告信息:java.lang.classnotfoundexception error ,这就意味着你的phptest.class文件不在你的java.class.path目录下。
  
    注意的是java是一种强制类型语言,而php不是,这样我们在将它们融合时,容易导致错误,于是我们在向java传递变量时,要正确指定好变量的类型。如:$myj->foo = (string) 12345678; or $myj->foo = "12345678";
  (www.iocblog.net 文章来源)
    这只是一个很小的例子,你可以创建你自己的java类,并使用php很好的调用它!(www.iocblog.net 文章来源)



文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。