最开始要装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,在浏览器中查看
会看到如下信息:
复制代码 代码如下:
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"; ?>
在浏览器中查看
方法二: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