From 13a7a1d7eb7ea1ef6ca67a2b3c1962c9402bd37d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20G=C5=82=C3=B3wka?= Date: Sat, 19 Feb 2022 05:19:07 +0100 Subject: [PATCH] BAEL-4234: example of JNI registerNatives() method (#11745) * BAEL-4234: example of JNI registerNatives() method * fixed formatting in the cpp file * removed camelcase in test package name --- ...ldung_jni_RegisterNativesHelloWorldJNI.cpp | 21 ++++++++++++++ ...aeldung_jni_RegisterNativesHelloWorldJNI.h | 29 +++++++++++++++++++ .../src/main/cpp/generateNativeLibMac.sh | 3 +- .../jni/RegisterNativesHelloWorldJNI.java | 18 ++++++++++++ .../JNIRegisterNativesManualTest.java | 26 +++++++++++++++++ 5 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 java-native/src/main/cpp/com_baeldung_jni_RegisterNativesHelloWorldJNI.cpp create mode 100644 java-native/src/main/cpp/com_baeldung_jni_RegisterNativesHelloWorldJNI.h mode change 100644 => 100755 java-native/src/main/cpp/generateNativeLibMac.sh create mode 100644 java-native/src/main/java/com/baeldung/jni/RegisterNativesHelloWorldJNI.java create mode 100644 java-native/src/test/java/com/baeldung/jni/registernatives/JNIRegisterNativesManualTest.java diff --git a/java-native/src/main/cpp/com_baeldung_jni_RegisterNativesHelloWorldJNI.cpp b/java-native/src/main/cpp/com_baeldung_jni_RegisterNativesHelloWorldJNI.cpp new file mode 100644 index 0000000000..37adb35333 --- /dev/null +++ b/java-native/src/main/cpp/com_baeldung_jni_RegisterNativesHelloWorldJNI.cpp @@ -0,0 +1,21 @@ +#include "com_baeldung_jni_RegisterNativesHelloWorldJNI.h" +#include + + +JNIEXPORT jstring JNICALL hello (JNIEnv* env, jobject thisObject) { + std::string hello = "Hello from registered native C++ !!"; + std::cout << hello << std::endl; + return env->NewStringUTF(hello.c_str()); +} + +static JNINativeMethod methods[] = { + {"sayHello", "()Ljava/lang/String;", (void*) &hello }, +}; + + +JNIEXPORT void JNICALL Java_com_baeldung_jni_RegisterNativesHelloWorldJNI_register (JNIEnv* env, jobject thsObject) { + jclass clazz = env->FindClass("com/baeldung/jni/RegisterNativesHelloWorldJNI"); + + (env)->RegisterNatives(clazz, methods, sizeof(methods)/sizeof(methods[0])); +} + diff --git a/java-native/src/main/cpp/com_baeldung_jni_RegisterNativesHelloWorldJNI.h b/java-native/src/main/cpp/com_baeldung_jni_RegisterNativesHelloWorldJNI.h new file mode 100644 index 0000000000..bcca780208 --- /dev/null +++ b/java-native/src/main/cpp/com_baeldung_jni_RegisterNativesHelloWorldJNI.h @@ -0,0 +1,29 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class com_baeldung_jni_RegisterNativesHelloWorldJNI */ + +#ifndef _Included_com_baeldung_jni_RegisterNativesHelloWorldJNI +#define _Included_com_baeldung_jni_RegisterNativesHelloWorldJNI +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: com_baeldung_jni_RegisterNativesHelloWorldJNI + * Method: sayHello + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_com_baeldung_jni_RegisterNativesHelloWorldJNI_sayHello + (JNIEnv *, jobject); + +/* + * Class: com_baeldung_jni_RegisterNativesHelloWorldJNI + * Method: register + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_com_baeldung_jni_RegisterNativesHelloWorldJNI_register + (JNIEnv *, jobject); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/java-native/src/main/cpp/generateNativeLibMac.sh b/java-native/src/main/cpp/generateNativeLibMac.sh old mode 100644 new mode 100755 index d11dcc7c01..834a07acc8 --- a/java-native/src/main/cpp/generateNativeLibMac.sh +++ b/java-native/src/main/cpp/generateNativeLibMac.sh @@ -3,4 +3,5 @@ g++ -c -fPIC -I${JAVA_HOME}/include -I${JAVA_HOME}/include/darwin com_baeldung_jni_HelloWorldJNI.cpp -o com_baeldung_jni_HelloWorldJNI.o g++ -c -fPIC -I${JAVA_HOME}/include -I${JAVA_HOME}/include/darwin com_baeldung_jni_ExampleParametersJNI.cpp -o com_baeldung_jni_ExampleParametersJNI.o g++ -c -fPIC -I${JAVA_HOME}/include -I${JAVA_HOME}/include/darwin com_baeldung_jni_ExampleObjectsJNI.cpp -o com_baeldung_jni_ExampleObjectsJNI.o -g++ -dynamiclib -o ../../../native/macos/libnative.dylib com_baeldung_jni_HelloWorldJNI.o com_baeldung_jni_ExampleParametersJNI.o com_baeldung_jni_ExampleObjectsJNI.o -lc \ No newline at end of file +g++ -c -fPIC -I${JAVA_HOME}/include -I${JAVA_HOME}/include/darwin com_baeldung_jni_RegisterNativesHelloWorldJNI.cpp -o com_baeldung_jni_RegisterNativesHelloWorldJNI.o +g++ -dynamiclib -o ../../../native/macos/libnative.dylib com_baeldung_jni_HelloWorldJNI.o com_baeldung_jni_ExampleParametersJNI.o com_baeldung_jni_ExampleObjectsJNI.o com_baeldung_jni_RegisterNativesHelloWorldJNI.o -lc \ No newline at end of file diff --git a/java-native/src/main/java/com/baeldung/jni/RegisterNativesHelloWorldJNI.java b/java-native/src/main/java/com/baeldung/jni/RegisterNativesHelloWorldJNI.java new file mode 100644 index 0000000000..c5461aafef --- /dev/null +++ b/java-native/src/main/java/com/baeldung/jni/RegisterNativesHelloWorldJNI.java @@ -0,0 +1,18 @@ +package com.baeldung.jni; + +public class RegisterNativesHelloWorldJNI { + + static { + System.loadLibrary("native"); + } + + public static void main(String[] args) { + RegisterNativesHelloWorldJNI helloWorldJNI = new RegisterNativesHelloWorldJNI(); + helloWorldJNI.register(); + helloWorldJNI.sayHello(); + } + + public native String sayHello(); + + public native void register(); +} diff --git a/java-native/src/test/java/com/baeldung/jni/registernatives/JNIRegisterNativesManualTest.java b/java-native/src/test/java/com/baeldung/jni/registernatives/JNIRegisterNativesManualTest.java new file mode 100644 index 0000000000..96e15bd822 --- /dev/null +++ b/java-native/src/test/java/com/baeldung/jni/registernatives/JNIRegisterNativesManualTest.java @@ -0,0 +1,26 @@ +package com.baeldung.jni.registernatives; + +import com.baeldung.jni.RegisterNativesHelloWorldJNI; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class JNIRegisterNativesManualTest { + @Before + public void setup() { + System.loadLibrary("native"); + } + + @Test + public void whenRegisteredNativeHelloWorld_thenOutputIsAsExpected() { + RegisterNativesHelloWorldJNI helloWorld = new RegisterNativesHelloWorldJNI(); + helloWorld.register(); + + String helloFromNative = helloWorld.sayHello(); + + assertNotNull(helloFromNative); + assertTrue(helloFromNative.equals("Hello from registered native C++ !!")); + } +}