PHP如何调用JAVA 类库

来源:互联网 发布:淘宝上营业执照是外地 编辑:程序博客网 时间:2024/06/17 23:49
<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>
 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很好的调用它!<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>