Moya's Blog

About

慈濟大陸賑災、緬甸風災捐款: External link mark
郵政劃撥帳號-06692433
戶名-佛教慈濟基金會賑災專戶
(註明四川震災或緬甸風災)
慈濟捐款網站請按此 External link mark


處順境若能慈悲,則後福自在,仆逆境倘得智慧,則遺禍自消,於順境若能不喜,則後患不至,當逆境或能捨得,則福自踵來。此慈悲喜捨,便是大般涅槃無上妙法,入世行者或能三思。


《既然清淨,何必有網?》
《譬如大日,遍照不爽。》
《上下十方,無為自在;》
《因陀羅手,去曼達礙。》


靜思晨語系列 獅子吼大德 慈濟的好友 雅虎的好友 老骨頭級的朋友

20 Mar 2008 - 15:37 in , tagged , , by MoyaTseng
對於在 PHP 上跑 Java 程式這件事情,有很多人非常有興趣瞭解,畢竟 PHP 在寫網頁的時候很容易,但是在開發大型的應用的時候,特別是一些具備 Static Status 的資料等等需求的時候,PHP 其實不是很好用。不然就是有想要大量利用既有的 Java Open Source 資源的時候,如果可以讓 PHP 直接採用,其實 PHP 程式開發的限制會小更多。

另外,喜歡 Java 的人,其實未必喜歡 JSP,畢竟和 PHP 比起來,JSP 還是略遜一籌,況且 PHP 的 Framework 像是 Symfony 之類的都已經相當成熟,開發起來更順暢了。還有,其實在 PHP 5 的程式語法就相當靠齊 Java 的語法了,等等都會讓人覺得 PHP 和 Java 一統是個趨勢。

既然有很多現實面的需求考量,甚至 IBM 等大廠都開始投入精神在這方面,我也就花一些時間稍微瞭解一下 PHP 和 Java 整合的可能性。就之前研究過的 Caucho 所開發的 Resin,採用 Quercus 在 Java 的平台上支援 PHP 程式的運作,只不過經過幾個版本的研究後,發現有一些潛在的問題,特別是對於 PHP 一些語法的支援不足,連 Symfony 這個 framework 都無法正常運作,所以放棄。

另外一個可能的方法,就是讓 PHP 直接去呼叫 Java 的部分,找了一下網路上的資料,發現了 PHP-Java-Bridge。雖然這個東西其實已經問世很久了,但是之前沒有需求,所以也沒有打聽,現在有需求,就研究了一下。Google 上找了一些文件來參考,發現他還滿有用的,可以單機直接執行,可以透過 Servelet,甚至 remote 到其他的機器上的 Servelet Container 等等。

以下主文部分直接由 Wiki 嵌入,日後直接修改 wiki 的主文這邊也會跟著修正。在整理資料的時候,這種嵌入主文的方式還滿好用的。整合 Blog 和 Wiki 的平台就可以一邊很容易的發表文章,另一邊可以簡單的分類彙整,還滿好用的。

簡單介紹一下這個東西的安裝與使用的方法:

Reference Sites

  1. PHP Java Bridge Sourceforge External link mark

Require

  1. Java 1.6
  2. PHP 5.1.4 以上 (我用目前最新的 PHP 5.2.5)
  3. Apache 2.0 (我用 Apache 2.2.8)

Install PHP-Java-Bridge

Download Project Tar Ball File

% fetch http://downloads.sourceforge.net/php-java-bridge/php-java-bridge_5.2.0.tar.gz

Setup & Install

  1. 主程式
    % tar xvfz php-java-bridge_5.2.0.tar.gz
    % cd php-java-bridge-5.2.0/
    % phpize && ./configure --with-java=/usr/local/jdk1.6.0 --disable-backend && make
    
  2. 相關 PHP scripts
    % mkdir -p /usr/local/share/php/java
    % cp php-java-bridge-5.2.0/server/META-INF/java/* /usr/local/share/php/java
    
  3. 複製 modules
    % cp php-java-bridge-5.2.0/modules/* /usr/local/lib/php/20060613/
    
  4. 複製 JavaBridge.jar
    % cp server/JavaBridge.jar /usr/local/lib/php/20060613/
    

設定 PHP extension

  • 在 /usr/local/etc/php/extensions.ini 當中增加
    extension=java.so
    

設定 php.ini

  • 增加 java 相關設定
    [java]
    java.java_home = /usr/local/jdk1.6.0/bin/
    java.java = /usr/local/jdk1.6.0/bin/java
    java.log_level = 2
    
  • 修改 include_path 為:
    include_path = ".:/usr/local/share/php:/usr/local/share/pear"
    

Usage

範例 1

  • test.php
    <?php
    
    require_once("java/Java.inc");
    
    $v = new Java("java.util.Vector");
    $v->add($buf=new Java("java.lang.StringBuffer"));
    $buf->append("100");
    echo $v->elementAt(0)->toString();
    
    ?>
    

範例 2

  • hello.java
    //------------------------------------------------------------------ 
    import java.util.*;
    import java.text.*;
    import java.math.*;
    
    public class hello
    {
      public static void main(String[] args)
      { 
        //do nothing - this will keep us from getting a compile error 
      } 
    
      public String SayHello()
      {
        Date today = new Date();
        return "Hello Geek here is the java date and time: " + today; 
      }
    
      public int SayNumber()
      {
        int mynum = 0;
    
        for (int i = 1; i <= 5; i++)
        {
           mynum =+ (int)(Math.random()*100);
        }
        return mynum;
      }
    
    } //------------------------------------------------------------------
    
  • test2.php
    <?php
    require_once("java/Java.inc");
    
    java_require("/usr/local/www/wlog/web");
    $obj = new Java("hello");
    
    // Call the "sayHello()" method
    $output = $obj->SayHello();
    echo $output.'  this text in PHP<br />'; // Displays (so this comes from the class!)
    
    // Call the "SayNumber()" method
    $output = $obj->SayNumber();
    
    // Displays (so this comes from the class!)
    echo $output.' is a lucky number'; 
    
    //Because the JVM caches everything we want reset while playing with the code. Otherwise the
    // a cached version of the same class file will be used rather than the new one
    echo "<br />Resetting back-end to initial state\n";
    
    // suppress the warning message from the use of reset.
    @java_reset();
    ?> 
    


Leave a Reply

You may have to login or register to comment if you haven't already.

訪客統計: 3548 人次



請按此訂閱每日人間菩提。靜思晨語 External link mark
本站所有言論均不代表慈濟基金會 External link mark


我為你祝福
我也要許願

r2 – 21 Mar 2008 – 15:33:10 – Main.MoyaTseng
Copyright © 1999-2009 by the contributing authors. All material on this collaboration platform is the property of the contributing authors. Ideas, requests, problems regarding Moya's Blog? Send feedback.