/**
* PHP调用java类配置过程
*
* @param
* @arrange 512-笔记网: www.jb51.cc
**/
<init-param>
<param-name>promiscuous</param-name>
<param-value>On</param-value>
</init-param>
/*** 来自编程之家 jb51.cc(jb51.cc) ***/
5. 和JavaBridge类似,PHP配置默认属性也是不允许远程文件的包含,要修改这个属性,就要编辑PHP.ini文件,将下面两个属性打开 allow_url_fopen = On allow_url_include = On(这个属性可能要添加)@H_301_0@@H_301_0@6. 在apache的目录/var/www/html/下面写一个测试PHP页面,内容如下:
/**
* PHP调用java类配置过程
*
* @param
* @arrange 512-笔记网: www.jb51.cc
**/
<?PHP
//require_once("/usr/local/tomcat/webapps/JavaBridge/java/Java.inc");
require_once("http://localhost:8080/JavaBridge/java/Java.inc");
$System = new Java("java.lang.System");
$te = $System->getProperties();
echo $te;
?>
/*** 来自编程之家 jb51.cc(jb51.cc) ***/
7. 在浏览器中访问http://localhost/test.PHP,如果能看到如下内容,则你的 apache 可以访问到本机的java类了。
{java.runtime.name=Java(TM) SE Runtime Environment,sun.boot.library.path=/usr/java/jdk1.6.0_17/jre/lib/i386,java.vm.version=14.3-b01,shared.loader=,java.vm.vendor=Sun Microsystems Inc.,java.vendor.url=http://java.sun.com/,path.separator=:,tomcat.util.buf.StringCache.byte.enabled=true,java.util.logging.config.file=/usr/local/tomcat/conf/logging.properties,java.vm.name=Java HotSpot(TM) Client VM,file.encoding.pkg=sun.io,sun.java.launcher=SUN_STANDARD,user.country=CN,sun.os.patch.level=unknown,java.vm.specification.name=Java Virtual Machine Specification,user.dir=/usr/local/tomcat/bin,java.runtime.version=1.6.0_17-b04,java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment,java.endorsed.dirs=/usr/local/tomcat/endorsed,os.arch=i386,java.io.tmpdir=/usr/local/tomcat/temp,line.separator=,java.vm.specification.vendor=Sun Microsystems Inc.,java.naming.factory.url.pkgs=org.apache.naming,java.util.logging.manager=org.apache.juli.ClassLoaderLogManager,os.name=Linux,sun.jnu.encoding=UTF-8,java.library.path=/usr/java/jdk1.6.0_17/jre/lib/i386/client:/usr/java/jdk1.6.0_17/jre/lib/i386:/usr/java/jdk1.6.0_17/jre/../lib/i386:/usr/java/packages/lib/i386:/lib:/usr/lib,java.specification.name=Java Platform API Specification,java.class.version=50.0,sun.management.compiler=HotSpot Client Compiler,os.version=2.6.11-1.1369_FC4,user.home=/root,catalina.useNaming=true,user.timezone=Asia/Shanghai,java.awt.printerjob=sun.print.PSPrinterJob,file.encoding=UTF-8,java.specification.version=1.6,catalina.home=/usr/local/tomcat,java.class.path=/usr/local/tomcat/bin/bootstrap.jar,user.name=root,java.naming.factory.initial=org.apache.naming.java.javaURLContextFactory,package.definition=sun.,java.,org.apache.catalina.,org.apache.coyote.,org.apache.tomcat.,org.apache.jasper.,java.vm.specification.version=1.0,java.home=/usr/java/jdk1.6.0_17/jre,sun.arch.data.model=32,user.language=zh,java.specification.vendor=Sun Microsystems Inc.,java.vm.info=mixed mode,sharing,java.version=1.6.0_17,java.ext.dirs=/usr/java/jdk1.6.0_17/jre/lib/ext:/usr/java/packages/lib/ext,sun.boot.class.path=/usr/java/jdk1.6.0_17/jre/lib/resources.jar:/usr/java/jdk1.6.0_17/jre/lib/rt.jar:/usr/java/jdk1.6.0_17/jre/lib/sunrsasign.jar:/usr/java/jdk1.6.0_17/jre/lib/jsse.jar:/usr/java/jdk1.6.0_17/jre/lib/jce.jar:/usr/java/jdk1.6.0_17/jre/lib/charsets.jar:/usr/java/jdk1.6.0_17/jre/classes,server.loader=,java.vendor=Sun Microsystems Inc.,catalina.base=/usr/local/tomcat,file.separator=/,java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport.cgi,common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar,sun.io.unicode.encoding=UnicodeLittle,sun.cpu.endian=little,package.access=sun.,sun.beans.,
@H_301_0@8. 下面自己写一个类,实现调用。内容如下:
/**
* PHP调用java类配置过程
*
* @param
* @arrange 512-笔记网: www.jb51.cc
**/
package test;
public class Test{
private String t1="hello world!";
public String get(){
return this.t1;
}
public static void main(String args[]){
Test t = new Test();
System.out.println(t.get());
}
}
/*** 来自编程之家 jb51.cc(jb51.cc) ***/
然后在和test目录同级的目录下写一个MANIFEST.MF文件,内容如下: Main-Class:(空格)test.Test(回车) MANIFEST.MF文件具有严格的结构,一定要按照这个格式写,如果包含了第三方包,一定要在第二行注明! 目录结构如下: Xxxx MANIFEST.MF test Test.java 然后在test目录同级执行jar cvfm test.jar MANIFEST.MF test/ 生成一个jar文件test.jar,用命令java –jar test.jar执行以下看是否可行。@H_301_0@@H_301_0@9. 修改test.PHP的内容如下
/**
* PHP调用java类配置过程
*
* @param
* @arrange 512-笔记网: www.jb51.cc
**/
<?PHP
//require_once("/usr/local/tomcat/webapps/JavaBridge/java/Java.inc");
require_once(http://localhost:8080/JavaBridge/java/Java.inc);
$System = new Java("java.lang.System");
$te = $System->getProperties();
echo $te;
java_require("/root/");//一定要把刚才生成的jar文件放到这个require的目录下面
$objectKey = new java("test.Test");
$tes =$objectKey->get();
echo $tes;
?>
/*** 来自编程之家 jb51.cc(jb51.cc) ***/
刷新http://localhost/test.PHP 你会看到比刚才看到的java环境内容多一行的Hello world内容。@H_301_0@@H_301_0@后记:test.PHP文件的require_once的参数,如果是本机访问tomcat,可以写绝对路径,不用http形式的,如果是这一行出现错误,不妨写成绝对路径试试看,如果出现错误,一般是PHP.ini或者你的web.xml文件的两处修改没有做。还有就是如果没有看到想要的内容或者有什么错误,记得先重启一下apache或者tomcat试试看,柳暗花明,呵呵。 对照我的记录,你会hello world的! 原文链接:https://www.f2er.com/php/528088.html