2015年5月4日 星期一

JNI coding

src:
http://blogforopensource.blogspot.tw/2012/08/jni-sample-java-code.html



notes:
15'5/5 :
- javah is used for JNI header
- pay attention to the c compile. It may need extra compiler like VC++




contents:

Blogging after a long time..


JNI - Java Native Interface.


Java interface to non­Java code.
It is Java's link to the "outside world"

•  Native methods are compiled into a dynamic link
  library (.dll, .so, etc.)
•  OS loads and links the library into the process
  that is running the Java Virtual Machine
•  Part of the Java Developer Kit(JDK),
  serves as a glue between Java side and
  native side of an application
• Allows Java code that runs inside a Java Virtual
  Machine (JVM) to inter operate with applications and
  libraries written in other programming languages,
  such as C, C++, and assembly

JNI w.r.t Operating System :


JNI Sequence:


If you are really interested in JNI,

You can find a Sample Code from here.

http://code.google.com/p/onlineexaminationsysteminjava/downloads/detail?name=samplejnicode.zip&can=2&q=

Reference:

http://patriot.net/~tvalesky/jninative.html
0 

Add a comment


-----------------------------------------------------------------------------------

Native Method example using JNI

Trivial JNI example
  1. Declare your native methods in an otherwise normal Java class.
    public class nativetest
    {
     public native String sayHello(String s);
     public static void main(String[] argv)
     {
      String retval = null;
      nativetest nt = new nativetest();
      retval = nt.sayHello("Beavis");
      System.out.println("Invocation returned " + retval);
     }
    }
    
  2. javac nativetest.java
    Standard Java compile
  3. javah -jni nativetest
    Generates .h files. Here is the code.
    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include 
    /* Header for class nativetest */
    
    #ifndef _Included_nativetest
    #define _Included_nativetest
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
     * Class:     nativetest
     * Method:    sayHello
     * Signature: (Ljava/lang/String;)Ljava/lang/String;
     */
    JNIEXPORT jstring JNICALL Java_nativetest_sayHello
      (JNIEnv *, jobject, jstring);
    
    #ifdef __cplusplus
    }
    #endif
    #endif
    
  4. Write your native code, using the .h file generated above Copy the function prototype from the .h file and paste it in.
    #include "nativetest.h" /*double quotes tells it to search current directory*/
    
    JNIEXPORT jstring JNICALL Java_nativetest_sayHello
      (JNIEnv *env, jobject thisobject, jstring js)
    
    {
     return js;
    }
    
  5. Compile
    cl -c /Ic:\jdk1.1.6\include /Ic:\jdk1.1.6\include\win32 nativetest.c 
    link /libpath=c:\jdk1.1.6\lib nativetest.obj /dll
    
    The above uses Microsoft Visual C++ command-line tools.
  6. Modify your Java class to load the library.
    public class nativetest
    {
     static {
      System.loadLibrary("nativetest");
     }
     public native String sayHello(String s);
     public static void main(String[] argv)
     {
      String retval = null;
      nativetest nt = new nativetest();
      retval = nt.sayHello("Beavis");
      System.out.println("Invocation returned " + retval);
     }
    }
    
  7. run the example
    C:\jni\hello>java nativetest
    Invocation returned Beavis
    


    notes:

    2015年5月2日 星期六

    Mose code


    src:
    http://nomad1999.tripod.com/general/d015.htm

    content:



    通訊知識
    文, 表, 程式:  Blackmore

    摩斯電碼進階 
    (這一整頁有點大, 還請大家耐心些, 訓練程式在最下面)
      
    摩斯電碼說明
    1.摩斯電碼的組成英碼每組通常為五字
    例: VCDSG GTTUU POPOQ DDBNL GTRDY TTFUU DDRTY QROOP SXCGY GTTEJ
    中碼(數字碼)通常每組四字
    例:5548 9038 4830 8402 4850 5848 4830 47302 5849 4820
    2.摩斯電碼的聲響只有長短音的分別  " . "  中文練習讀音為"滴"  " - "中文練習讀音為"答"
    而電碼長短之時間如下:
    一個字 如 Z " --..
    " 的時間為一個單位  " " 的時間為三個單位 
    . "和 " - "的間隔時間為三個單位
     
    字和字 如 GR 為 --.  .-.        這時字和字間格的時間為三個單位
     
    字組和字組 GTRED  POQAZ  時間間格為五個時間單位
      
    詳細摩斯電碼表
    .  中文練習時念做"滴"       -  中文練習時念做"答"
    摩斯電碼及電話讀音法
    Letter摩斯電碼EnglishAmerican InternationalNATOAmerican (police)
    A.-AndrewAbelAmsterdamAlfaAdam
    B-...BenjaminBakerBaltimoreBravoBoy
    C-.-.CharlieCharlieCasablancaCharlieCharlie
    D-..DavidDogDenmarkDeltaDavid
    E.EdwardEasyEdisonEchoEdward
    F..-.FrederickFoxFloridaFoxtrotFrank
    G--.GeorgeGeorgeGallipoliGolfGeorge
    H....HarryHowHavanaHotelHenry
    I..IsaacItemItalyIndiaIda
    J.---JackJigJerusalemJulietJohn
    K-.-KingKingKilogramKiloKing
    L.-..LucyLoveLiverpoolLimaLincoln
    M--MaryMikeMadagascarMikeMary
    N-.NelliNanNew YorkNovemberNora
    O---OliverOboeOsloOscarOcean
    P.--.PeterPeterParisPapaPaul
    Q--.-QueenieQueenQuebecQuebecQueen
    R.-.RobertRogerRomaRomeoRobert
    S...SugarSugarSantiagoSierraSam
    T-TommyTareTripoliTangoTom
    U..-UncleUncleUppsalaUniformUnion
    V...-VictorVictorValenciaVictorVictor
    W.--WilliamWilliamWashingtonWhiskyWilliam
    X-..-XmasX (eks)XanthippeX-rayX-ray
    Y-.--YellowYokeYokohamaYankeeYoung
    Z--..ZebraZebraZürichZuluZebra
     
    數字及標點符號
    數字短碼長碼標點符號Code
    1.-.----句點 。.-.-.-
    2..-..---逗點 ,--..--
    3...--...--Colon---...
    4....-....-問號  ?..--..
    5..........撇號 ‵.----.
    6-....-....連字號  --....-
    7--...--...Fraction bar-..-.
    8-..---..括號(   )-.--.-
    9-.----.引號『 』.-..-.
    0------  
    訓練程式
    在上面的input欄輸入英文字母或文字,選擇速度後按下play即可
    英碼型式:FFKRP FKEOD SSKPQ XZLKF KOAQG KPLYO
    中碼型式:1245 6543 5533 6672 0954 3367 9707 1273 0836
    你也可以下載回去練習(內容包含練習表格,電報來去電紙)

    完成日期99-08-26, 最後修改日期99-08-26
    註: 本文是轉錄自Blackmore先生原放置於男人殺手站的文章
    此文獲得Blackmore先生同意轉載於此Nomad僅做校對與重新排版的工作
    在此特別再一次的感謝Blackmore 先生能授權本站轉載這部分的內容~~