PHP如何调用JAVA 类库

来源:互联网 发布:java ssm 开源项目 编辑:程序博客网 时间:2024/06/06 00:44
<script type="text/javascript">google_ad_client = "pub-8800625213955058";/* 336x280, 创建于 07-11-21 */google_ad_slot = "0989131976";google_ad_width = 336;google_ad_height = 280;//</script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>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[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很好的调用它!
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 灯饰批发哪里好 做灯饰的厂家 中山市琪朗灯饰厂有限公司 中山灯饰厂招聘信息 灯饰批发厂 中山古镇灯饰厂100排名 水晶灯灯饰厂 中山古镇灯饰厂 五金灯饰厂 重庆灯饰厂 灯饰厂好做吗 灯饰五金厂 邓世昌的英雄事迹 智能照明厂家 工业照明厂家 照明灯具生产厂家 灯具批发厂家直销 工业照明灯具厂家 灯饰设计图片 客厅灯饰装修效果图 灯饰定制 国际灯饰 安华灯饰城 郑州灯饰城批发市场在哪里 石家庄灯饰城 无锡灯饰城 正定灯饰城 郑州灯饰城 罗村灯饰城 中山市古镇灯饰城 中山古镇灯饰城地址 涞水灯饰城 佛山灯饰城 广州灯饰城 重庆灯饰城 北京的灯具城 天津灯饰城 北京灯饰城批发市场 北京灯具城批发市场在哪里 灯具制造厂家 羽毛球场照明灯具