Java Native Interface ¿¹Á¦

author : Yoon Kyung Koo(yoonforh@yahoo.com)
Copyright (c) 1999 Yoon Kyung Koo, All rights reserved.

ÀÚ¹Ù¿¡¼­ c³ª c++·Î ÀÛ¼ºµÈ ³×ÀÌƼºê ¶óÀ̺귯¸® ÇÔ¼ö¸¦ È£ÃâÇϱâ À§Çؼ­´Â
ÀÚ¹Ù ³×ÀÌƼºê ÀÎÅÍÆäÀ̽º(JNI)¸¦ »ç¿ëÇØ¾ß ÇÕ´Ï´Ù.
´ÙÀ½Àº À©µµ¿ì (WIN32) ½Ã½ºÅÛÀÇ ¸Þ¸ð¸® »ç¿ë·®À» °Ë»çÇÏ´Â API ÇÔ¼ö¸¦ ÀÚ¹Ù¿¡¼­ »ç¿ëÇÏ´Â ¿¹Á¦ÀÔ´Ï´Ù.

»ç¿ëµÇ´Â ÆÄÀÏ

JNI ±¸Çö ¼ø¼­

  1. ¸ÕÀú ÀÚ¹Ù ¼Ò½º ÆÄÀÏÀ» ÀÛ¼ºÇÕ´Ï´Ù. MemoryStatus.java ÆÄÀÏ¿¡ º¸¸é native·Î ¼±¾ðµÈ ¸Þ¼ÒµåµéÀÌ ÀÖ½À´Ï´Ù. ÀÌ ¸Þ¼ÒµåµéÀº C ¶óÀ̺귯¸®¿¡¼­ ±¸ÇöµË´Ï´Ù.
  2. ÀÚ¹Ù ¼Ò½º¸¦ ÄÄÆÄÀÏÇÕ´Ï´Ù.
    javac MemoryStatus.java
  3. JNI ½ºÅ¸ÀÏÀÇ Çì´õ ÆÄÀÏÀ» »ý¼ºÇÕ´Ï´Ù.
    javah -jni MemoryStatus
    ¸¸µé¾îÁø Çì´õ ÆÄÀÏÀº MemoryStatus.h¶ó´Â À̸§À» °¡Áý´Ï´Ù.
  4. JNI ½ºÅ¸ÀÏ Çì´õ ÆÄÀÏÀ» ÀÎŬ·çµåÇÏ´Â ½ÇÁ¦ ±¸Çö ¶óÀ̺귯¸®¸¦ ÀÛ¼ºÇÕ´Ï´Ù. ¿©±â¿¡¼­´Â WinMemory.c°¡ ÀÚ¹ÙÀÇ ³×ÀÌƼºê ¸Þ¼ÒµåµéÀ» ±¸ÇöÇÏ°í ÀÖ½À´Ï´Ù.
  5. À©µµ¿ìÀÇ ³×ÀÌƼºê ¶óÀ̺귯¸®¸¦ ÄÄÆÄÀÏÇÕ´Ï´Ù. Àû´çÇÑ MakefileÀ» ¸¸µé¾î »ç¿ëÇÏ´Â °ÍÀÌ Æí¸®ÇÕ´Ï´Ù.
    Makefile ¾øÀÌ µµ½º â¿¡¼­ ¹Ù·Î ÄÄÆÄÀÏÇÏ·Á¸é
    set JAVA_HOME=C:\jdk1.2 (JDK°¡ ¼³Ä¡µÈ µð·ºÅ丮)
    cl -I%JAVA_HOME%\include -I%JAVA_HOME%\include\win32 -LD WinMemory.c -Fewinmem.dll
    ¿Í °°ÀÌ Á÷Á¢ ¸í·ÉÀ» ÁÖ¸é µË´Ï´Ù.
  6. Âü°í·Î ÀÛ¼ºµÈ winmem.dll ÆÄÀÏÀ» ÀÚ¹Ù °¡»ó ¸Ó½ÅÀÌ ÀоîµéÀÌ´Â °ÍÀº MemoryStatus.java ¼Ò½º Äڵ忡 ÀÖ´Â System.loadLibrary("winmem"); ·çƾÀÔ´Ï´Ù.

The order of JNI implementation

  1. First, compose a Java source file, MemoryStatus.java. In that file, you can see methods declared as native. Those methods will be implemented in a C library.
  2. compile the Java code.
    javac MemoryStatus.java
  3. generate JNI header file.
    javah -jni MemoryStatus
    The generated header file will have the name of MemoryStatus.h.
  4. Compose the real implementation library which includes above generated header file. In this case, WinMemory.c implements Java's native methods.
  5. Compile native library. A simple Makefile will help you.
    If you want to compile in dos prompt without Makefile, then do as follows.
    set JAVA_HOME=C:\jdk1.2 (the directory where JDK locates)
    cl -I%JAVA_HOME%\include -I%JAVA_HOME%\include\win32 -LD WinMemory.c -Fewinmem.dll
  6. for your information, where Java Virtual Machine loads winmem.dll is the routine System.loadLibrary("winmem") in MemoryStatus.java.

ÀÌ ÆäÀÌÁö´Â 1999³â 2¿ù 22ÀÏ¿¡ óÀ½ ¸¸µé¾îÁ³½À´Ï´Ù.
Æ©Å丮¾ó ÆäÀÌÁö·Î µ¹¾Æ°¡±â