Monday, July 20, 2009

7 Cm Renal Cyst Kidney

Accessing native methods (JNI vs. JNA) Restructuring the Blog

.

This chapter is being developed, may contain errors .

Java provides the ability to access functions written in C through what is called "native methods." There are currently two technologies through which we can call C functions from our Java programs. These are:
  • JNI - "Java Native Interface (conventional technology), and
  • JNA -" Java Native Access "
While both allow you to access C functions from Java, the methodology is very different . JNI is extremely more complex than JNA.

This chapter will develop some examples in both technologies with the objective to understand how they could be used and compared.


HelloWorld - Use JNI JNI

is somewhat cumbersome. Below I will list the steps to be performed to invoke a C function that prints "Hello World" in the console.

If the reader believes that this is very difficult or complicated to follow him until the end I recommend you go straight to the implementation developed with JNA exposed below.

1 - Define a Java class with native methods

CLib.java (class with native methods)
   1:  
2: demo.jni package;
3:
4: public class Cliba
5: {
6: / / the methods native not be develop
7: public native void HelloWorld ( ) ;
8: }
9:


2 - Compile the. Java before and then run the command javah to generate the . h with the C headers that correspond to the native methods defined in the class.

javah-jni demo.jni.CLib

We should note that to run this command from the command line we have seteado the PATH to% JAVA_HOME% \\ bin and must be positioned in the parent directory or demo package While we must have the CLASSPATH set correctly.

javah command should generate the following file:

demo_jni_CLib.h
   1:   / *          DO         NOT         EDIT         THIS         FILE         -         it         is         machine         generated         */   
2: # include < jni.h >
3: /* Header for class demo_jni_CLib * /
4:
5: # ifndef _Included_demo_jni_CLib
6: # define _Included_demo_jni_CLib
7: # ifdef __cplusplus
8: external " C" {
9: # endif
10: / *
11: * Class: demo_jni_CLib
12: * Method: holaMundo
13: * Signature: ()V
14: */
15: JNIEXPORT void JNICALL Java_demo_jni_CLib_holaMundo
16: ( JNIEnv *, jobject ) ;
17:
18: # ifdef __cplusplus
19:
} 20: # endif
21: # endif
22:


3 - Programming in C native method implementation HelloWorld using the header we got after running the command javah.

demoJNI.c
   1:  
2: # include < stdio.h >
3: # include < jni.h >
4: # include " demo_jni_CLib.h "
5:
6: JNIEXPORT void JNICALL Java_demo_jni_CLib_holaMundo ( JNIEnv * env , jobject obj )
7: {
8: printf ( " Hello World \\ n " ) ;
9:
} 10:


4 - Compile the C code and generate a DLL. If we use MinWG the command is as follows:

gcc-c-I% JAVA_HOME \\ include-I% JAVA_HOME% \\ include \\ win32 demoJNI.c
gcc-shared-o demoJNI.dll
demoJNI.o
This generates the file demoJNI.dll.


5 - Write a program that instantiates the class and invoke the method Cliba HelloWorld.

Demo.java
       1:   
2: package demo.jni;
3:
4: public class Demo
5: {
6: public static void main ( String[] args )
7: {
8: System. loadLibrary ( " demoJNI " ) ;
9: CLib clib = new Cliba ( ) ;
10: clib. HelloWorld ( ) ;
11:}
12:}
13:

More information: http://java.sun.com/ docs / books / jni / html / start.html

Hello World - JNA

The approach provides JNA is more intuitive, simple and direct. We just need to download the file jna.jar .

1 - JNA began to write the C code header unconditionally with any stranger.

demoJNA.c
   1:  
2: # include \u0026lt;stdio.h >
3: void HelloWorld ( )
4:
{5: printf ( " Hello World \\ n " ) ;
6:
} 7:


2 - The next step is to create a Java interface that defines the HelloWorld method that "wrappea" function written in C.

CLib.java
   1:  
2: demo.jna package;
3:
4: import com.sun.jna.Library;
5 :
6: public interface extends Cliba Library
7: {
8: public void HelloWorld ( ) ;
9:
} 10:


3 - Finally we can write the main program that invokes the interface HelloWorld Cliba.

Demo.java
   1:  
2: demo.jna package;
3:
4: import com.sun.jna.Native;
5 :
6: public class Demo
7: {
8: public static void main ( String [] args )
9: {
10: Cliba = clib ( Cliba ) Native. LoadLibrary ( " demoJNA " , Cliba. class ) ;
11: clib. HelloWorld ( ) ;
12:}
13:}
14:

More information: https: / / jna.dev.java. net /












.

0 comments:

Post a Comment