Skip to content

Commit 62b5a59

Browse files
charropauljervis
authored andcommitted
BAEL-1637: Guide to JNI(Java Native Interface) (eugenp#4066)
* BAEL-1546: Java 8 Math additions * Applied feedback to Unit Tests * BAEL-1546 Added missing test annotations * Added code for BAEL-1637 * Added script for Windows C++ code compile * Added compilation script for MacOS * Added some Unit tests
1 parent 1b7361a commit 62b5a59

18 files changed

Lines changed: 331 additions & 0 deletions
19.4 KB
Binary file not shown.

‎jni/native/macos/libnative.dylib‎

22.5 KB
Binary file not shown.

‎jni/native/win32/native.dll‎

1.52 MB
Binary file not shown.

‎jni/pom.xml‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>com.baeldung</groupId>
4+
<artifactId>jni</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
7+
<dependencies>
8+
<dependency>
9+
<groupId>junit</groupId>
10+
<artifactId>junit</artifactId>
11+
<version>4.8.1</version>
12+
<scope>test</scope>
13+
</dependency>
14+
</dependencies>
15+
</project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "com_baeldung_jni_ExampleObjectsJNI.h"
2+
#include <iostream>
3+
4+
/*
5+
* Class: com_baeldung_jni_ExampleObjectsJNI
6+
* Method: createUser
7+
* Signature: (Ljava/lang/String;D)Lcom/baeldung/jni/UserData;
8+
*/
9+
JNIEXPORT jobject JNICALL Java_com_baeldung_jni_ExampleObjectsJNI_createUser
10+
(JNIEnv *env, jobject thisObject, jstring name, jdouble balance){
11+
12+
// Create the object of the class UserData
13+
jclass userDataClass = env->FindClass("com/baeldung/jni/UserData");
14+
jobject newUserData = env->AllocObject(userDataClass);
15+
16+
// Get UserData fields to set
17+
jfieldID nameField = env->GetFieldID(userDataClass , "name", "Ljava/lang/String;");
18+
jfieldID balanceField = env->GetFieldID(userDataClass , "balance", "D");
19+
20+
// Set the values of the new object
21+
env->SetObjectField(newUserData, nameField, name);
22+
env->SetDoubleField(newUserData, balanceField, balance);
23+
24+
// Return the created object
25+
return newUserData;
26+
}
27+
28+
/*
29+
* Class: com_baeldung_jni_ExampleObjectsJNI
30+
* Method: printUserData
31+
* Signature: (Lcom/baeldung/jni/UserData;)Ljava/lang/String;
32+
*/
33+
JNIEXPORT jstring JNICALL Java_com_baeldung_jni_ExampleObjectsJNI_printUserData
34+
(JNIEnv *env, jobject thisObject, jobject userData){
35+
36+
// Find the class method id
37+
jclass userDataClass = env->GetObjectClass(userData);
38+
jmethodID methodId = env->GetMethodID(userDataClass, "getUserInfo", "()Ljava/lang/String;");
39+
40+
// Call the object method and get the result
41+
jstring result = (jstring)env->CallObjectMethod(userData, methodId);
42+
43+
// Print the result
44+
std::cout << "C++: User data is: " << env->GetStringUTFChars(result, NULL) << std::endl;
45+
46+
return result;
47+
}
48+

‎jni/src/main/cpp/com_baeldung_jni_ExampleObjectsJNI.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.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "com_baeldung_jni_ExampleParametersJNI.h"
2+
#include <iostream>
3+
#include <string>
4+
5+
/*
6+
* Class: com_baeldung_jni_ExampleParametersJNI
7+
* Method: sumIntegers
8+
* Signature: (II)J
9+
*/
10+
JNIEXPORT jlong JNICALL Java_com_baeldung_jni_ExampleParametersJNI_sumIntegers (JNIEnv* env, jobject thisObject, jint first, jint second){
11+
std::cout << "C++: The numbers received are : " << first << " and " << second << std::endl;
12+
return (long)first + (long)second;
13+
}
14+
15+
16+
/*
17+
* Class: com_baeldung_jni_ExampleParametersJNI
18+
* Method: sayHelloToMe
19+
* Signature: (Ljava/lang/String;Z)Ljava/lang/String;
20+
*/
21+
JNIEXPORT jstring JNICALL Java_com_baeldung_jni_ExampleParametersJNI_sayHelloToMe (JNIEnv* env, jobject thisObject, jstring name, jboolean isFemale){
22+
const char* nameCharPointer = env->GetStringUTFChars(name, NULL);
23+
std::cout << "C++: The string received is: " << nameCharPointer << std::endl;
24+
std::string title;
25+
if(isFemale){
26+
title = "Ms. ";
27+
}
28+
else{
29+
title = "Mr. ";
30+
}
31+
32+
std::string fullName = title + nameCharPointer;
33+
return env->NewStringUTF(fullName.c_str());
34+
}

‎jni/src/main/cpp/com_baeldung_jni_ExampleParametersJNI.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.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "com_baeldung_jni_HelloWorldJNI.h"
2+
#include <iostream>
3+
4+
/*
5+
* Class: com_baeldung_jni_HelloWorldJNI
6+
* Method: sayHello
7+
* Signature: ()Ljava/lang/String;
8+
*/
9+
JNIEXPORT jstring JNICALL Java_com_baeldung_jni_HelloWorldJNI_sayHello (JNIEnv* env, jobject thisObject) {
10+
std::string hello = "Hello from C++ !!";
11+
std::cout << hello << std::endl;
12+
return env->NewStringUTF(hello.c_str());
13+
}

‎jni/src/main/cpp/com_baeldung_jni_HelloWorldJNI.h‎

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

0 commit comments

Comments
 (0)