Skip to content

Commit 4b62cbf

Browse files
authored
BAEL-4234: example of JNI registerNatives() method (eugenp#11745)
* BAEL-4234: example of JNI registerNatives() method * fixed formatting in the cpp file * removed camelcase in test package name
1 parent 69a8035 commit 4b62cbf

5 files changed

Lines changed: 96 additions & 1 deletion

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "com_baeldung_jni_RegisterNativesHelloWorldJNI.h"
2+
#include <iostream>
3+
4+
5+
JNIEXPORT jstring JNICALL hello (JNIEnv* env, jobject thisObject) {
6+
std::string hello = "Hello from registered native C++ !!";
7+
std::cout << hello << std::endl;
8+
return env->NewStringUTF(hello.c_str());
9+
}
10+
11+
static JNINativeMethod methods[] = {
12+
{"sayHello", "()Ljava/lang/String;", (void*) &hello },
13+
};
14+
15+
16+
JNIEXPORT void JNICALL Java_com_baeldung_jni_RegisterNativesHelloWorldJNI_register (JNIEnv* env, jobject thsObject) {
17+
jclass clazz = env->FindClass("com/baeldung/jni/RegisterNativesHelloWorldJNI");
18+
19+
(env)->RegisterNatives(clazz, methods, sizeof(methods)/sizeof(methods[0]));
20+
}
21+

java-native/src/main/cpp/com_baeldung_jni_RegisterNativesHelloWorldJNI.h

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

java-native/src/main/cpp/generateNativeLibMac.sh

100644100755
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
g++ -c -fPIC -I${JAVA_HOME}/include -I${JAVA_HOME}/include/darwin com_baeldung_jni_HelloWorldJNI.cpp -o com_baeldung_jni_HelloWorldJNI.o
44
g++ -c -fPIC -I${JAVA_HOME}/include -I${JAVA_HOME}/include/darwin com_baeldung_jni_ExampleParametersJNI.cpp -o com_baeldung_jni_ExampleParametersJNI.o
55
g++ -c -fPIC -I${JAVA_HOME}/include -I${JAVA_HOME}/include/darwin com_baeldung_jni_ExampleObjectsJNI.cpp -o com_baeldung_jni_ExampleObjectsJNI.o
6-
g++ -dynamiclib -o ../../../native/macos/libnative.dylib com_baeldung_jni_HelloWorldJNI.o com_baeldung_jni_ExampleParametersJNI.o com_baeldung_jni_ExampleObjectsJNI.o -lc
6+
g++ -c -fPIC -I${JAVA_HOME}/include -I${JAVA_HOME}/include/darwin com_baeldung_jni_RegisterNativesHelloWorldJNI.cpp -o com_baeldung_jni_RegisterNativesHelloWorldJNI.o
7+
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
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.jni;
2+
3+
public class RegisterNativesHelloWorldJNI {
4+
5+
static {
6+
System.loadLibrary("native");
7+
}
8+
9+
public static void main(String[] args) {
10+
RegisterNativesHelloWorldJNI helloWorldJNI = new RegisterNativesHelloWorldJNI();
11+
helloWorldJNI.register();
12+
helloWorldJNI.sayHello();
13+
}
14+
15+
public native String sayHello();
16+
17+
public native void register();
18+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.jni.registernatives;
2+
3+
import com.baeldung.jni.RegisterNativesHelloWorldJNI;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertNotNull;
8+
import static org.junit.Assert.assertTrue;
9+
10+
public class JNIRegisterNativesManualTest {
11+
@Before
12+
public void setup() {
13+
System.loadLibrary("native");
14+
}
15+
16+
@Test
17+
public void whenRegisteredNativeHelloWorld_thenOutputIsAsExpected() {
18+
RegisterNativesHelloWorldJNI helloWorld = new RegisterNativesHelloWorldJNI();
19+
helloWorld.register();
20+
21+
String helloFromNative = helloWorld.sayHello();
22+
23+
assertNotNull(helloFromNative);
24+
assertTrue(helloFromNative.equals("Hello from registered native C++ !!"));
25+
}
26+
}

0 commit comments

Comments
 (0)