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:

    沒有留言:

    張貼留言