PHP 如何调用 JAVA 类库

 PHP 如何调用 JAVA 类库
  作者:游荡 | 浏览:244 | 评论:0

  JAVA 是个非常强大的编程利器,它的扩展库也是非常的有用,这篇教程,主要讲述怎样使用 PHP 调用功能强大的 JAVA 类库 (classes)。为了方便你的学习,这篇教程将包括 JAVA 的安装及一些基本的例子。

  Windows下的安装

  第一步:安装 JDK,这是非常容易的,你只需一路回车的安装好。然后做好以下步骤。

  在 Win9x 下加入 :“PATH=%PATH%;C:/jdk1.2.2/bin” 到 AUTOEXEC.BAT 文件中。

  在 NT 下加入 “ ;C:/jdk1.2.2/bin”到环境变量中。

  这一步是非常重要的,这样 PHP 才能正确的找到需调用的 JAVA 类。

  第二步:修改你的 PHP.INI 文件。

[java]
extension=php_java.dll
java.library.path=c:/web/php4/extensions/
java.class.path="c:/web/php4/extensions/jdk1.2.2/php_java.jar;c:/myclasses"
java.home = "d:/jdk1.2.2/jre"
java.library = "d:/jdk1.2.2/jre/bin/server/jvm.dll"

注:PHP5 没有稳定的 Java 扩展,所以按照上面配置,将导致 Apache 崩溃--深空。

  在 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>/n"
;
print
"Java vendor=".$system->getProperty("java.vendor")." <p>/n/n"
;
print
"OS=".$system->getProperty("os.name")." "
.
$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"
;

?>

  如果你正确安装了,你将会看到以下信息:

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; < 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";

  这只是一个很小的例子,你可以创建你自己的 JAVA 类,并使用 PHP 很好的调用它!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值