php&java的简单使用


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"

在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; i < args.length; i++) {
String arg = args;
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很好的调用它!

 

 

本文作者:
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3