Site Search :
Standard Enterprise XML Methodology Pattern Setting Tunning Other
Article Contributors
GuestBook
Javapattern Maven
XSourceGen Dev
JetSpeed Test
JLook Image
jLook Family Site


native method Á¤ÀÇ ¹× ±¸Çö
 
native method Á¤ÀÇ, ÀýÂ÷º° ±¸Çö ¹æ¹ý ¼³¸í ( 2003/05/06 ) 276
Written by specular - ÀüÈ«¼º
1 of 1
 


¡Ø Java Native Interface
JNI´Â native method¸¦ ÀÌ¿ëÇؼ­ C/C++ÀÇ code¸¦ Java¿¡¼­ ÀÌ¿ëÇÒ¼ö ÀÖ´Â ¹æ¹ýÀÌ´Ù. 
native ¸Þ¼Òµå¸¦ Á¤ÀÇÇؼ­, »ç¿ëÇÏ´Â ¹æ¹ý¿¡ ´ëÇØ ¼Ò°³¸¦ ÇÏ°Ú½À´Ï´Ù. 

1. Example Java Source

C/C++ÀÇ code¸¦ ÀÚ¹Ù¿¡¼­ ºÒ·¯ »ç¿ëÇÏ·Á¸é, native method¸¦ Á¤ÀÇÇØ¾ß Çϴµ¥, ¾Æ·¡ ÄÚµå¿Í °°ÀÌ 
method body¸¦ °®Áö¾Ê´Â method¸¦ native keyword¸¦ ÅëÇØ Á¤ÀÇÇÑ´Ù. 
ÀÌ°ÍÀº methodÀÇ body°¡ C/C++ codeÀÇ dll(Unix¿¡¼­´Â so) ÆÄÀÏ·Î µÇ¾î ÀÖ´Ù´Â ÀǹÌÀÌ´Ù. 
±×·¡¼­ runtime¿¡´Â ÀÌ dll ÆÄÀÏÀ» ¸Þ¸ð¸®¿¡ loadingÀ» Çؾ߸¸ method¸¦ ½ÇÇàÇÒ¼ö°¡ Àִµ¥, 
dll ÆÄÀÏÀ» loadingÇÏ´Â library´Â System classÀÇ loadLibrary() ¸Þ¼ÒµåÀÌ´Ù.
classpath°æ·Î¿¡¼­ ÆĶó¹ÌÅÍÀÇ ÆÄÀÏÀ» ¸Þ¸ð¸®¿¡ loadingÀ» ÇÏÁÒ.  

public class Hello {

    public static native void print();

    static {
        System.loadLibrary("hello");
    }

    public static void main(String args[]) {
      Hello.print();
    }
}

compile
prompt>javac Hello.java

compileµÈ class¸¦ ½ÇÇàÇÏ¸é ´ç¿¬È÷ ¿¡·¯°¡ ¹ß»ýµÇ°ÚÁÒ. ´ÙÀ½°ú °°ÀÌ.
prompt>java Hello
Exception in thread "main" java.lang.UnsatisfiedLinkError: no hello in java.libr
ary.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1312)
at java.lang.Runtime.loadLibrary0(Runtime.java:749)
at java.lang.System.loadLibrary(System.java:820)
at Hello.(Hello.java:6)

2. C/C++ÀÇ Header file »ý¼º

native method¸¦ C/C++·Î ±¸Çö Çϱâ À§Çؼ­´Â Java method ¼±¾ðÀ» C/C++ÀÇ method¼±¾ðÀ¸·Î
mapping½ÃÄÑÁÖ¾î¾ß °ÚÁÒ. 
ÀÌ°ÍÀº javah ¶ó´Â  ¸í·É¾î¸¦ ÀÌ¿ëÇÕ´Ï´Ù. 
±×·¡¼­, ´ÙÀ½°ú °°Àº ¸í·É¾î·Î C/C++ÀÇ Header ÆÄÀÏÀ» »ý¼ºÇÕ´Ï´Ù.

prompt>javah -jni -classpath . -o Hello.h Hello

//»ý¼ºµÈ header file : Hello.h 
/* DO NOT EDIT THIS FILE - it is machine generated */
#include 
/* Header for class Hello */

#ifndef _Included_Hello
#define _Included_Hello
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:     Hello
* Method:    print
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_Hello_print
(JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif

3. C code »ý¼ºÀ» À§ÇÑ C/C++ compiler install


»ý¼ºµÈ header file¿¡ Á¤ÀÇµÈ JNIEXPORT void JNICALL Java_Hello_print(JNIEnv *, jclass); ¸Þ¼Òµå¸¦ 
±¸ÇöÇϱâ À§ÇØ Borland C++ Compiler5.5 ¸¦ ´Ù¿î ¹Þ¾Æ ¼³Ä¡ÇÑ´Ù.

http://www.borland.com/downloads/ ¼³Ä¡¹æ¹ýÀº ÀνºÅç ÆÄÀÏÀ» ½ÇÇàÇϸé, ¼³Ä¡ µð·ºÅ丮¿¡ readme fileÀÌ Àִµ¥, bcc32.cfg, ilink32.cfg ÆÄÀÏÀ» readme ÆÄÀÏÀÇ ³»¿ë°ú °°ÀÌ ¸¸µé¾î bin dir¿¡ ÀúÀåÇÏ¸é ³¡³­´Ù. 4. C source code Hello.h ÆÄÀÏ¿¡ ¼±¾ðµÈ ¸Þ¼Òµå¸¦ ´ÙÀ½°ú °°ÀÌ ±¸ÇöÇÑ´Ù. //hello.cpp file #include "Hello.h" JNIEXPORT void JNICALL Java_Hello_print (JNIEnv *, jclass) { printf("Hello World"); } 5. compile and dll file »ý¼º ´ÙÀ½°ú °°Àº ¸í·É¾î·Î compileÇؼ­ hello.dll ÆÄÀÏÀ» »ý¼ºÇÑ´Ù. prompt>bcc32 -c -Id:\java\jdk1.3\include -Id:\java\jdk1.3\include\win32 hello.cpp prompt>bcc32 -tWD hello.obj 6. ½ÇÇà hello.dll ÆÄÀÏÀÌ ÇöÀç µð·ºÅ丮¿¡ ¸¸µé¾î Á³±â ¶§¹®¿¡, System.loadLibrary()·Î ¸Þ¸ð¸®¿¡ loadingÇÒ¼ö ÀÖ°í, print() ¶ó´Â native method°¡ ½ÇÇàµÉ¼ö ÀÖ°ÚÁÒ. prompt>java Hello Hello World 2001.06.20 written by Jeon HongSeong
 
1
References
 
Copyright ¨Ï 2003 www.javapattern.info & www.jlook.com, an jLOOK co.,LTD