顯示具有 [語言][Java]筆記 標籤的文章。 顯示所有文章
顯示具有 [語言][Java]筆記 標籤的文章。 顯示所有文章

2013年3月28日 星期四

php-java bridge 筆記

為了在php使用weka的函式庫

最近用了一個方便的東西:PHP/Java Bridge

它的運作原理暫時還不是很了解

先筆記一下要怎麼安裝和使用

[安裝]

下載點(版本:6.2.1/2013/03/28)

下載後是一個壓縮檔,裡面有兩個檔案,documentation和JavaBridge.war(WAR file format)

建立一個資料夾JavaBridge(自己命名),將JavaBridge.war丟進去

打開cmd,cd至JavaBridge

java -classpath JavaBridge.war TestInstallation

執行完後出現兩個資料夾:ext, java
                        兩個檔案:RESULT.html, test.php

ext內有四個jar檔:JavaBridge.jar, php-script.jar, php-servlet.jar,script-api.jar

java內有一個Java.inc

滿身大汗戰戰兢兢地弄到這步後,怎麼知道成功了沒?

讓我們把焦點移至test.php

這個php檔案裡面已經有幫寫好的測試,它大致上要做的是先列出phpinfo

然後呼叫java.lang.System.getProperties()列出環境資訊

所以假設http://localhost:8080/JavaBridge/test.php有先列出phpinfo後再列出java 環境資訊大概就是成功了。

不過Ref 1中的第5點有提到 
Copy JavaBridge.jar and php-script.jar to your J2SE/Java SE ext directory ({JAVA_HOME}/jre/lib/ext).
不曉得是否為必須動作

Ref:
1. http://www.developer.com/java/other/php-with-java-using-php-java-bridge-tutorial.html
2. http://blog.wabow.com/archives/62
3. http://www.fkblog.org/blog562

[使用外部jar檔]

Ref 2中有提到
開發者似乎在version 6.0時已經將java_require( )給拿掉
所以6.0後的版本,利用java_require(weka.jar)這樣的方式會出現warning
參考Ref 1中的一個解法


  1. package your code to jar, and copy it to java.ext.dirs which you can found in JavaBridge.log
  2. copy the related class libraries to java.ext.dirs
  3. restart the service of JavaBridge
意思就是將weka.jar丟到jre的ext資料夾內然後重新啟動JavaBridge服務


Ref:
1. http://stackoverflow.com/questions/5389689/working-with-php-java-bridge
2. PHP/Java Bridge FAQ

[使用方式]

在php檔中宣告java的Instance
$s = new Java("java.lang.String", "我在php中運行java");
echo $s;


第一個參數填入欲使用類別,就像import一樣
第二個參數以後填入的是此類別Constructor的參數,
若是Constructor有一個以上的參數,目前測試的結果是依序填下去就可以


$fvNominalVal = new Java("weka.core.FastVector",2);
$fvNominalVal->addElement("yes");
$fvNominalVal->addElement("no");
//Constructor have two parameters
$Identity = new Java("weka.core.Attribute","Identity", $fvNominalVal); 

使用函式的方法要照php的方式來用
原本在java使用函式為 fvNominalVal.addElement("yes");
要改成  $fvNominalVal->addElement("yes");

將Java的Instance轉換為相對應的php value
Ref 1中提到
Use java_values() to convert a Java object into an equivalent PHP value

若要做反向的動作
Use java_closure() to convert a PHP object into an equivalent Java object.

Ref:
1. PHP/Java Bridge API

2013年1月12日 星期六

抽象類別Abstract Class

[概念]
pic 1:polymorphism

每位超級英雄有自己的穿衣風格,蜘蛛人簡單地穿上紅藍蜘蛛衣,
蝙蝠俠回家換上蝙蝠裝,鋼鐵人拿出箱子,換上鋼鐵裝備,

//Base Class
public class SuperHero{
  public void power(){
    System.out.println("superhuman strength");
  }
  public void vest(){
    //each hero has their own style
  }
}

//Sub Class
public class Spiderman extends SuperHero{
  public void power(){
    System.out.println("spin a web");
  }
  public void vest(){
    System.out.println("simply change his equipment");
  }
}

public class Batman extends SuperHero{
  public void power(){
    System.out.println("shadow his body");
  }
  public void vest(){
    System.out.println("go cave and put on his equipment");
  }
}

public class Ironman extends SuperHero{
  public void power(){
    System.out.println("energy repulsors");
  }
  public void vest(){
    System.out.println("take out his box and put on his equipment");
  }
}

SuperHero這個Base Class並不需要特別實作vest(),
只需要交給個別的Sub Class去實作自己的vest(),
在這情況下,定義了所謂的抽象方法(Abstract Method),意即不特別去實作方法的內容,
一個類別中若包含抽象方法,則可稱為抽象類別(Abstract Class),
需要注意的是,不能生成抽象類別的物件,但可以宣告抽象類別的變數


//Base Class
public abstract class SuperHero{
  public void power(){
    System.out.println("superhuman strength");
  }
  public abstract void vest(); //abstract method
}

//Sub Class
public class Spiderman extends SuperHero{
  public void power(){
    System.out.println("spin a web");
  }
  public void vest(){
    System.out.println("simply change his equipment");
  }
}

public class Batman extends SuperHero{
  public void power(){
    System.out.println("shadow his body");
  }
  public void vest(){
    System.out.println("go cave and put on his equipment");
  }
}

public class Ironman extends SuperHero{
  public void power(){
    System.out.println("energy repulsors");
  }
  public void vest(){
    System.out.println("take out his box and put on his equipment");
  }
}

public class Test{
  public static void main(String[] args){
    SuperHero sh1 = new Spiderman();
    SuperHero sh2 = new Batman();
    SuperHero sh3 = new Ironman();

    sh1.vest(); //"simply change his equipment"
    sh2.vest(); //"go cave and put on his equipment"
    sh3.vest(); //"take out his box and put on his equipment"
  }
}


[細節]

[延伸]

[參考]
1. 良葛格學習筆記

如有錯誤,請不吝指教

多型Polymorphism

[概念]
多型作用於Instance Method上,不作用在Class Method、Class Variable和Instance Variable上

目的是為了在執行時期讓Sub Class的物件動態地選擇合適的method

pic 1: polymorphism

//Base Class
public class SuperHero{
  power(){
    System.out.println("superhuman strength");
  }
  fortune(){
    System.out.println("fortune type");
  }
}

//Sub Class
public class Spiderman extends SuperHero{
  power(){
    System.out.println("spin a web");
  }
}

public class Test{
  public static void main(String[] args){
    SuperHero sh = new Spiderman();
    sh.power();    //"spin a web"
    sh.fortune();  //"fortune type"
  }
}

變數sh宣告為SuperHero型態(稱為型式型態),但實際型態卻為Spiderman

sh.power( )會執行實際型態的method,也就是
power(){
    System.out.println("spin a web");
}


[細節]

[延伸]
抽象類別Abstract Class
介面Interface


[參考]
1. O'Reilly技術短文 OO

如有錯誤,請不吝指教

繼承Inheritance

[概念]
物件導向重要的概念之一

Sub Class繼承Base Class

//Base Class
public class Human{
  height
  weight
  
  walk(); 
  eat();  
}

//Sub Class
public class Ken extends Human{
  code();
  playguitar();
}

一般來說,Instance Variable、Instance Method、Class Variable、Class Method都會被繼承


[細節]
public, protected, private, package的影響

Base Class和Sub Class的Instance Variable名稱相同時會發生甚麼事

Method Overriding

[延伸]
多型Polymorphism
抽象類別Abstract Class
介面Interface

[參考]
1. O'Reilly技術短文 OO
2. 良葛格學習筆記

如有錯誤,請不吝指教