如何调用类库计划

2021-08-24 淮安装修公司

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

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

在 Win9x 下加入 :“PATH=%PATH%;C:\\jdk1.2.2\\bin” 到T文件中

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

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

第二步:修改你的I文件。

[java]

extension=php_l

th=c:\\web\\php4\\extensions\\

th=\"c:\\web\\php4\\extensions\\jdk1.2.2\\php_r;c:\\myclasses\"

在I中加入extension=php_l,并在[java]中,设定好th,让它指向php_r,如果你使用新的JAVA类,你也应该存入这个路径,在这篇例子中,我们使用c:\\myclasses这个目录。

第三步:测试环境,创建如下PHP文件:

<?php

$system = new Java(\"stem\");

print \"Java version=\".$system->getProperty(\"rsion\").\" <br>\\n\";

print \"Java vendor=\".$system->getProperty(\"ndor\").\" <p>\\n\\n\";

print \"OS=\".$system->getProperty(\"\").\" \".

$system->getProperty(\"rsion\").\" on \".

$system->getProperty(\"ch\").\" <br>\\n\";

$formatter = new Java(\"mpleDateFormat\",\"EEEE,

MMMM dd, yyyy \'\'at\'\' h:mm:ss a zzzz\");

print $formatter->format(new Java(\"te\")).\"\\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类非常容易。新建一个va文件,将它放置在你的th目录下,文件内容如下:

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(uals(\"\")) {

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(ngth == 0) {

String arg = \"\";

intln(st(arg));

}else{

for (int i=0; i < ngth; i++) {

String arg = args[i];

intln(st(arg));

}

}

}

}

创建这个文件后,我们要编译好这个文件,在DOS命令行使用javac va这个命令。

为了使用PHP测试这个JAVA类,我们创建一个p文件,内容如下:

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

?>

如果你得到这样的警告信息:assNotFoundException error ,这就意味着你的ass文件不在你的th目录下。

注意的是JAVA是一种强制类型语言,而PHP不是,这样我们在将它们融合时,容易导致错误,于是我们在向JAVA传递变量时,要正确指定好变量的类型。如:$myj->foo = (string) ; or $myj->foo = \"\";

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

南昌治疗妇科习惯性流产哪家好
东营白癜风哪里治疗好
南充看白癜风的医院
为你推荐