zoukankan      html  css  js  c++  java
  • windows下安装配置phpjavabridge,PHP调用自己的JAVA文件

    方法一:(推荐方法 ) 使用php/java桥 JavaBridge.jar 

    转自:http://zhengdl126.iteye.com/blog/418574

    http://sourceforge.net/projects/php-java-bridge 

    http://mirror.optus.net/sourceforge/p/ph/php-java-bridge/php-java-bridge_5.2.2_j2ee.zip

    最开始要装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 http://sourceforge.net/project/showfiles.php?group_id=117793 )


    將下載回來的 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:AppServphpext)

    把該檔建立捷徑,把建立好的捷徑放到啟動裡(這裡是C:Documents and SettingsAll 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,http://localhost/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-java-bridge_3.0.8_j2ee.zip (7 MB)

  • 相关阅读:
    Bash Shellshock(CVE-2014-6271)破壳漏洞测试
    极客时间-左耳听风-程序员攻略-分布式架构经典图书和论文
    极客时间-左耳听风-程序员攻略-分布式架构入门
    极客时间-左耳听风-程序员攻略-数据库
    极客时间-左耳听风-程序员攻略-Java底层知识
    Hackertarget:一款发现攻击面的工具
    解决tomcat was unable to start within问题
    当我们安装使用时,会出现eclipse启动不了,出现“Java was started but returned exit code=13......”的问题
    Eclipse启动报错:A java runtime Environment(JRE) or java Development……的解决办法
    eclipse svn插件安装方法
  • 原文地址:https://www.cnblogs.com/blogsme/p/3081621.html
Copyright © 2011-2022 走看看