Skip to content

Latest commit

ย 

History

History
66 lines (33 loc) ยท 3.27 KB

JNI&JNA.md

File metadata and controls

66 lines (33 loc) ยท 3.27 KB

JNI(Java Native Interface)

  • Virtual Machine์—์„œ ์‹คํ–‰๋˜๋Š” java ํ”„๋กœ๊ทธ๋žจ์ด ํ•ด๋‹น ํ”Œ๋žซํผ์˜ native code(C/C++)์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ์ œ๊ณตํ•˜๋Š” interface๋ฅผ ๋œปํ•จ.

  • C/C++๋กœ ๊ตฌํ˜„๋œ ๋™์  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ์—ฐ๋™ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•จ

  • JNI๋ฅผ ํ†ตํ•ด C/C++ dll ์‚ฌ์šฉํ•˜๊ธฐ

    ์ž๋ฐ”์—์„œ ๊ณต์‹์ ์œผ๋กœ ์ง€์›ํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋กœ, ์ •ํ•ด์ง„ ํ˜•ํƒœ๋กœ c++ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ œ์ž‘ํ•˜๊ฑฐ๋‚˜ JNI ๊ทœ๊ฒฉ์„ ๋”ฐ๋ฅด๋ฉด์„œ ๊ธฐ์กด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” C++ ๋ž˜ํ•‘ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ œ์ž‘ํ•ด์•ผ ํ•จ

    • STEP1 : native code๋กœ java ์†Œ์Šค ์ฝ”๋“œ ์ž‘์„ฑ

    • STEP2 : ์†Œ์Šค ์ปดํŒŒ์ผํ•ด์„œ * .class ํŒŒ์ผ ์ƒ์„ฑ

    • STEP3 : javah๋ฅผ ์ด์šฉํ•ด์„œ dll prototype์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š” * .h ํŒŒ์ผ ์ž๋™ ์ƒ์„ฑ

      • javah๋กœ ํ—ค๋” ์ƒ์„ฑํ•˜๋Š” ๊ณผ์ •์—์„œ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ
        • Error: Could not find class file for 'ํŒจํ‚ค์ง€๋ช….ํด๋ž˜์Šค๋ช…'.

          stackoverflow์˜ ๊ธ€์„ ๋ณด๊ณ  ํ•ด๊ฒฐ ์™„๋ฃŒ : ์ฐธ๊ณ  ๋งํฌ โœจ

    • STEP4 : ๋งŒ๋“ค์–ด์ง„ * .h ํŒŒ์ผ์„ C/C++ dll ํ”„๋กœ์ ํŠธ์— ์ถ”๊ฐ€ํ•˜์—ฌ ํ•ด๋‹น ํ•จ์ˆ˜ ๊ตฌํ˜„ ๋ฐ ์ปดํŒŒ์ผ (dll ์ƒ์„ฑ)

    • STEP5 : java์—์„œ System.LoadLibrary๋ฅผ ํ†ตํ•ด dll ๋ถˆ๋Ÿฌ์˜ค๊ณ  ํ•ด๋‹น ํ•จ์ˆ˜ ์‚ฌ์šฉ

      • ์‹คํ–‰ ์‹œ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ

        • Error : java.lang.UnsatisfiedLinkError ไธญ B)Ljava/lang/String;

JNA(Java Native Access)

  • JNI์—์„œ ์ง„ํ–‰ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ native code๋กœ ํ•จ์ˆ˜ ๊ตฌํ˜„, header ํŒŒ์ผ ์ƒ์„ฑ

  • ์ปค๋ฎค๋‹ˆํ‹ฐ์—์„œ ์ œ์ž‘ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ, ๊นƒํ—ˆ๋ธŒ์—์„œ ๋‹ค์šด๋กœ๋“œ ๋ฐ›๊ณ  ํ”„๋กœ์ ํŠธ์— ํฌํ•จ์‹œ์ผœ์•ผ ํ•จ

    JNI์™€๋Š” ๋‹ฌ๋ฆฌ ๋ž˜ํ•‘ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋งŒ๋“ค์ง€ ์•Š์•„๋„ ๊ฐ„ํŽธํ•˜๊ฒŒ ๊ธฐ์กด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ž๋ฐ”์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•จ

    • STEP1 : ๊นƒํ—ˆ๋ธŒ์—์„œ ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‹ค์šด๋กœ๋“œ โ†’ github ๋งํฌ โœจ

    • STEP2 : ๋‹ค์šด๋ฐ›์€ jna.jar๋ฅผ ๋นŒ๋“œ์— ํฌํ•จ์‹œํ‚ค๊ณ , ํ•ด๋‹น dll์— ์žˆ๋Š” ํ•จ์ˆ˜ ์‚ฌ์šฉ

์ฐธ๊ณ  โญ