php&java(二)
来源:互联网 发布:域名备案 网站备案 编辑:程序博客网 时间:2024/06/05 09:22
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
创建你自己的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很好的调用它!<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
例子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很好的调用它!<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- php&java(二)
- java程序员学习php(二)
- PHP画图(二)
- PHP安全(二)
- PHP作图(二)
- php学习(二)
- PHP基础(二)
- php理论学习(二)
- php基础知识(二)
- PHP基础知识(二)
- PHP基础知识(二)
- 初学php(二)
- PHP面试(二)
- PHP面试(二)
- php 之 (二)
- php基础(二)
- php函数(二)
- php(二)
- C#定时器的使用
- PHP 高手之路(一)
- 利用.net的强大功能发送email
- php&java(三)
- .NET中加密与解密QueryString的方法
- php&java(二)
- 详解:如何在.NET中访问MySQL数据库?
- WIN98下Apache1.3.14+PHP4.0.4的安装
- 看看一个.net版对话框控件
- 程序员路在何方
- php&java(一)
- 一个无刷新效果定时自动更新页面的例子
- PHP个人网站架设连环讲(三)
- ASP.NET2.0,无刷新页面新境界