当前位置: 移动技术网 > IT编程>开发语言>PHP > php调用自己java程序的方法详解

php调用自己java程序的方法详解

2017年12月12日  | 移动技术网IT编程  | 我要评论

本文实例讲述了php调用自己的java程序实现方法。分享给大家供大家参考,具体如下:

最开始要装jdk这个就不用说了,我装的是java ee 5+jdk

1.把下载的php-java-bridge_5.2.2_j2ee.zip解压出来,里面有个javabridge.war 直接用winrar打开,到web-inf/lib/javabridge.jar 把这个jar包拷到 你的php目录的ext/下。

2.打开war包,里面有个java的文件夹,把他全部拷到你的php项目下,如/demo/java

3.目前的版本是vmbridge了,要php调用java类,要先启动javabridge,

命令行下调用java –jar javabridge.jar或者双击javabridge.jar,在弹出的窗口中选择监听端口8080

为了以后启动方便,我在ext/下新建了一个bat文件内容如下:

@echo off
start javaw -jar javabridge.jar

保存后,双击启动 会有一个提示框选择vmbridge port 默认8080,直接点ok就行了

4.在/demo/下新建test.php内容如下:

<?php
require_once("java/java.inc");
header("content-type:text/html; charset=utf-8″);
// get instance of java class java.lang.system in php
$system = new java('java.lang.system');
$s = new java("java.lang.string", "php-java-bridge config…<br><br>");
echo $s;
// demonstrate property access
print 'java version='.$system->getproperty('java.version').' <br>';
print 'java vendor=' .$system->getproperty('java.vendor').' <br>';
print 'os='.$system->getproperty('os.name').' '.
$system->getproperty('os.version').' on '.
$system->getproperty('os.arch').' <br>';
// java.util.date example
$formatter = new java('java.text.simpledateformat',
"eeee, mmmm dd, yyyy 'at' h:mm:ss a zzzz");
print $formatter->format(new java('java.util.date'));
?>

5.启动apache,在浏览器中查看 http://localhost/demo/test.php

会看到如下信息:

复制代码 代码如下:
php-java-bridge config…
java version=1.6.0_10
java vendor=sun microsystems inc.
os=windows vista 6.0 on x86
星期日, 十一月 23, 2008 at 4:31:49 下午 中国标淮时间

自定义jar:

package ttt;
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));
   }
  }
 }
}

生成为jar,拷贝到d盘下。

/demo/index2.php

<?
require_once("java/java.inc");
java_require("d://1.jar");
$myj = new java("ttt.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";
?>

在浏览器中查看 http://localhost/demo/index2.php

方法二:php_java.dll 需要配置php.ini,新版的php-java-bridge都没有dll文件

首先确定你的php和apache伺服器及jdk(or jre也可)都已安装完成

上网下载php-java-bridge(自行找戴点 or )

将下载回来的 php-java-bridge解压缩,解压后文件夹里会有一个javabridge.war,再同样将这个javabridge.war解压缩(win rar即可解)
解压后可从 web-inf文件夹里的cgi文件夹找到java-x86-windows.dll,及web-inf文件夹里的lib文件夹找到javabridge.jar

将java-x86-windows.dll和javabridge.jar 複制到php的外挂文件夹(我这边是c:/appservphp/ext),并将java-x86-windows.dll改成php_java.dll

修改php.ini档案

如果php.ini原本没有以下内容,请自行加上,如果原本就有以下内容,请修改成如下[我使用的是jdk]

extension=php_java.dll

[java]
;java.java = "c:\jdk1.6.0_13\bin\java"
java.class.path = "d:\php\ext\javabridge.jar;c:\myclasses" c:\myclasses可自定义,用来存放自己写的java文件 
java.java_home = "c:\jdk1.6.0_13\jre"
java.library = "d:\jdk1.2.2\jre\bin\server\jvm.dll"
java.library.path = "d:\php\ext"

重新起动apache,查看phpinfo

java
java support  enabled
java bridge  3.0.8
java.java_home  c:\jdk1.6.0_13
java.java  c:\jdk1.6.0_13\bin\java
java.log_file  <stderr>
java.log_level  no value (use backend's default level)
java.ext_java_compatibility  off
java command  c:\jdk1.6.0_13\bin\java -djava.library.path=d:\php\ext -djava.class.path=d:\php\ext/javabridge.jar -djava.awt.headless=true php.java.bridge.javabridge inet_local:0 2
java status  running
java server  9267

看倒数第二项 java status的状态是不是not running (这是因为你没有启动javabridge.jar)。如果变成running <—-代表javabridge.jar已启动,已可正式使用php-java-bridge

如果没有启动则执行:

因为不可能每次开机都手动去启动javabridge.jar

所以我们写一个批处理,内容如下

@echo off
start javaw -jar javabridge.jar

把它存成phpjavabridge.bat,同样放在php的外挂文件夹里(这里是c:appserv/php/ext)

把该档建立捷径,把建立好的捷径放到启动里(这里是c:/documents and settings/all users/「开始」/功能表/程序启动)

这样一来,以后每次开机后就会自动启动c:appservphpext 文件夹里的phpjavabridge.bat

简单范例

<?
$system=new java('java.lang.system');
echo "java版本".$system->getproperty('java.version')."<br>";
echo "发行厂商".$system->getproperty('java.vendor')."<br>";
echo "作业系统版本".$system->getproperty('os.name')."<br>";
echo "java版本".$system->getproperty('os.version')."<br>";
echo "java版本".$system->getproperty('os.arch')."<br>";
?>

或者在php-java-bridge中找到test.php,查看效果

<?php
$system=new java("java.lang.system");
print "java version=".$system->getproperty("java.version")." <br>";
?>

[java]
extension=php_java.dll
java.library.path=c:webphp4extensions
java.class.path="c:webphp4extensionsjdk1.2.2php_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>nn";
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很重要,下一步我们就要创建自己的java文件,让php来调用,java文件的java.class.path很重要

创建和使用你自己的java类 [注意大小写]

创建你自己的 java 类非常容易。新建一个 phptest.java 文件,将它放置在你的 java.class.path 目录下【 c:\myclasses】,文件内容如下:

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 类,我们在web目录下创建一个 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 很好的调用它!关键在于理解java.class.path目录的重要性。

更多关于php相关内容感兴趣的读者可查看本站专题:《php数组(array)操作技巧大全》、《php排序算法总结》、《php常用遍历算法与技巧总结》、《php数据结构与算法教程》、《php程序设计算法总结》、《php数学运算技巧总结》、《php正则表达式用法总结》、《php运算与运算符用法总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网