Last active
          November 13, 2024 20:57 
        
      - 
      
- 
        Save gauravssnl/fe0c217e0ca42bb01b59cc93c417973e to your computer and use it in GitHub Desktop. 
Revisions
- 
        gauravssnl revised this gist Nov 13, 2024 . 2 changed files with 10 additions and 2 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,9 +5,13 @@ class Test { public static void main(String[] args) { System.out.println("Test OK"); System.out.println(stringFromJNI()); } public static String message() { return "Hello from Java"; } public native static String stringFromJNI(); } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,8 +2,7 @@ void println_java(JNIEnv *env, const char *msg); extern "C" jint JNI_OnLoad(JavaVM *jvm, void *) { JNIEnv *env = nullptr; jvm->GetEnv((void **)&env, JNI_VERSION_1_6); println_java(env, "Hello from JNI!"); @@ -24,3 +23,8 @@ void println_java(JNIEnv *env, const char *msg) { jstring str = env->NewStringUTF(msg); env->CallVoidMethod(out, mid, str); } extern "C" JNIEXPORT jstring JNICALL Java_Test_stringFromJNI(JNIEnv *env, jobject thiz) { return env->NewStringUTF("Hello String from JNI"); } 
- 
        gauravssnl created this gist Nov 13, 2024 .There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ class Test { static { System.loadLibrary("hello"); } public static void main(String[] args) { System.out.println("Test OK"); } public static String message() { return "Hello from Java"; } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ #include "jni.h" void println_java(JNIEnv *env, const char *msg); // extern "C" [[gnu::visibility("default")]] [[gnu::used]] jint JNI_OnLoad(JavaVM *jvm, void *) { JNIEnv *env = nullptr; jvm->GetEnv((void **)&env, JNI_VERSION_1_6); println_java(env, "Hello from JNI!"); return JNI_VERSION_1_6; } void println_java(JNIEnv *env, const char *msg) { // Get system class jclass syscls = env->FindClass("java/lang/System"); // Lookup the "out" field jfieldID fid = env->GetStaticFieldID(syscls, "out", "Ljava/io/PrintStream;"); jobject out = env->GetStaticObjectField(syscls, fid); // Get PrintStream class jclass pscls = env->FindClass("java/io/PrintStream"); // Lookup printLn(String) jmethodID mid = env->GetMethodID(pscls, "println", "(Ljava/lang/String;)V"); // Invoke the method jstring str = env->NewStringUTF(msg); env->CallVoidMethod(out, mid, str); } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ export CPLUS_INCLUDE_PATH="$JAVA_HOME/include:$JAVA_HOME/include/linux" clang hello.cpp -shared -o libhello.so javac Test.java && java -Djava.library.path=. Test