Hello JNI

๋งฅ์—์„œ Hello JNI!๋ฅผ ์ถœ๋ ฅํ•˜๋Š” Java ์‘์šฉํ”„๋กœ๊ทธ๋žจ์„ ๊ฐœ๋ฐœํ•ด ๋ณด์ž. ๋‹จ ์ด ์‘์šฉํ”„๋กœ๊ทธ๋žจ์€ ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•˜๋Š” ๋ถ€๋ถ„์„ C++ ๋กœ ์ž‘์„ฑํ•  ๊ฒƒ์ด๋‹ค.

๋„ค์ดํ‹ฐ๋ธŒ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” Java ํด๋ž˜์Šค ์ž‘์„ฑํ•˜๊ธฐ

$ vi HelloJNI.java
// ์ฃผ์˜! ํ•™์Šต ํŽธ์˜๋ฅผ ์œ„ํ•ด์„œ ํŒจํ‚ค์ง€๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š๋Š”๋‹ค!

class HelloJNI {
    private native void print();
    public static void main(String[] args) {
        new HelloJNI().print();
    }
    static {
        System.loadLibrary("HelloJNI");
    }
}

java ์ฝ”๋“œ ํŒŒ์ผ์„ ์ปดํŒŒ์ผํ•˜์—ฌ classํŒŒ์ผ์„ ๋งŒ๋“ ๋‹ค.

$ javac HelloJNI.java

JNI C++ ํŒŒ์ผ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ javah๋ฅผ ์ด์šฉํ•ด C++ ํ—ค๋”ํŒŒ์ผ์„ ๋งŒ๋“ ๋‹ค.

$ javah -jni HelloJNI

๋งŒ๋“ค์–ด์ง„ ํŒŒ์ผ์„ ํ™•์ธํ•ด ๋ณด์ž

$ cat HelloJNI.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloWorld */

#ifndef _Included_HelloJNI
#define _Included_HelloJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     HelloJNI
 * Method:    print
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_HelloJNI_print
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

JNI ํ•จ์ˆ˜๋ช…๊ณผ ์ธ์žํƒ€์ž… ๋“ฑ์€ ๊ด€๋ จ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•œ๋‹ค. Java_HelloJNI_print ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

$ vi HelloJNI.cpp
#include <jni.h>
#include <iostream>
#include "HelloJNI.h"

JNIEXPORT void JNICALL Java_HelloJNI_print
(
    JNIEnv  *env, 
    jobject  thiz
)
{
    std::cout << "Hello JNI!" << std::endl;
}

HelloJNI ๋„ค์ดํ‹ฐ๋ธŒ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ๋Š” jni.h ํŒŒ์ผ์˜ ์œ„์น˜๋ฅผ ์•Œ์•„์•ผ ํ•œ๋‹ค. ๋งฅ์—์„œ๋Š” ์•„๋ž˜์˜ ์œ„์น˜์— ์žˆ๋‹ค.

$ ls /System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers/

์ปดํŒŒ์ผํ•ด ๋ณด์ž

$ g++ "-I/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers/" -c HelloJNI.cpp

์ปดํŒŒ์ผํ•˜๋ฉด HelloJNI.o ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ๋‹ค. ์ด์ œ ๋™์ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ช…์€ ๋ฐ˜๋“œ์‹œ lib์œผ๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•˜๊ณ  ํ™•์žฅ์ž๋Š” jnilib ์ด์—ฌ์•ผ ํ•œ๋‹ค. ์œ„ ์ปดํŒŒ์ผ๋ฌธ์—์„œ jni.h๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†๋‹ค๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด jni.h ๊ฐ€ ์žˆ๋Š” ๊ณณ์„ ์ฐพ์•„์„œ ์ ์–ด์ค˜์•ผ ํ•œ๋‹ค. ์•„๋ž˜์ฒ˜๋Ÿผ ์‹ฌ๋ณผ๋ง ๋งํฌ๋ฅผ ์ง์ ‘ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด /System/Library/Java/JavaVirtualMachines/Headers ํด๋”๋ฅผ ๋งŒ๋“  ํ›„ ํ•ด๋‹น ํด๋”๋กœ ์ด๋™ํ•œ ๋‹ค์Œ ์•„๋ž˜์˜ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

$ sudo ln -s /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/System/Library/Frameworks/JavaVM.framework/Versions/A/Headers/jni.h jni.h
$ ln -s /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/System/Library/Frameworks/JavaVM.framework/Versions/A/Headers/jni_md.h jni_md.h
$ g++ -dynamiclib -o libhellojni.jnilib HelloJNI.o

์‹คํ–‰ํ•œ๋‹ค

$ java HelloJNI
Hello JNI!

๋งŒ์•ฝ ์•„๋ž˜์™€ ๊ฐ™์€ ๋งํฌ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์‹คํ–‰ํ•  ๋•Œ ๋„ค์ดํ‹ฐ๋ธŒ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์žˆ๋Š” ์œ„์น˜๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค.

Exception in thread "main" java.lang.UnsatisfiedLinkError: no HelloJNI in java.library.path
$ java -Djava.library.path="." HelloJNI

Last updated

Was this helpful?