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
This commit is contained in:
Maciej Główka 2022-02-19 05:19:07 +01:00 committed by GitHub
parent 4bb39341c1
commit 13a7a1d7eb
5 changed files with 96 additions and 1 deletions

View File

@ -0,0 +1,21 @@
#include "com_baeldung_jni_RegisterNativesHelloWorldJNI.h"
#include <iostream>
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]));
}

View File

@ -0,0 +1,29 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* 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

3
java-native/src/main/cpp/generateNativeLibMac.sh Normal file → Executable file
View File

@ -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
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

View File

@ -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();
}

View File

@ -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++ !!"));
}
}