How To Use JNI As A Bridge Between Java and C++ - by Sumit Datta - The Startup - Medium
How To Use JNI As A Bridge Between Java and C++ - by Sumit Datta - The Startup - Medium
Open in app
Member-only story
Search Medium
Many of the older software being used in Banking, Insurance and other industries have
been developed using programming languages such as C++, COBOL. Modernizing such
software — for e.g., containerizing and running them on Cloud — is a big challenge
because these are mostly procedural programs running as a monolith. At the same
time, you can’t really afford to rewrite the entire business logic in another
programming language like Java or Python or NodeJS. One path many people explore
is to use JNI to create a bridge between Java-based Spring Boots and existing C++
classes that has the business logic. While digging into the Internet, I found very few
articles on JNI with actual sample codes that went beyond Hello World. Hence this
article.
https://medium.com/p/1378ca904202 1/16
17/5/23, 11:31 How to use JNI as a bridge between Java and C++ | by Sumit Datta | The Startup | Medium
package com.test;
public class JavaServer {
public static void main(String[] args) {
// TODO Auto-generated method stub
JavaServer client = new JavaServer();
RequestData request = new RequestData();
request.setRadius(5);
request.setShape("Hemisphere");
ResponseData response = client.calculatevolume_in_cplusplus(request);
System.out.println("Response-> " + "Volume-> " + response.getVolume()
+ " Status->" + response.getStatus());
}
static {
System.loadLibrary("nativevolumecalculator");
}
public JavaServer() {
System.out.println("JavaServer created");
}
public com.test.ResponseData
calculatevolume_in_java(com.test.RequestData request) {
System.out.println("Java -> Calculate Volume called");
response.setVolume((4*22*request.getRadius()*request.getRadius()*reque
st.getRadius())/(3*7));
}
else {
// Assume the shape is Hemisphere
response.setVolume((2*22*request.getRadius()*request.getRadius()*reque
st.getRadius())/(3*7));
}
response.setStatus("OK");
https://medium.com/p/1378ca904202 2/16
17/5/23, 11:31 How to use JNI as a bridge between Java and C++ | by Sumit Datta | The Startup | Medium
return response;
}
Once you write the Java code, you have to generate the corresponding C++ header file
for the native function. To do so, put the following plugin configuration inside your
POM file in Maven.
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<arg>-h</arg>
<arg>.</arg>
</compilerArgs>
</configuration>
</plugin>
Maven will generate a C++ header file (com_test_JavaServer.h) with following content.
#ifndef _Included_com_test_JavaServer
#define _Included_com_test_JavaServer
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_test_JavaServer
* Method: calculatevolume_in_cplusplus
* Signature: (Lcom/test/RequestData;)Lcom/test/ResponseData;
*/
https://medium.com/p/1378ca904202 3/16
17/5/23, 11:31 How to use JNI as a bridge between Java and C++ | by Sumit Datta | The Startup | Medium
#ifdef __cplusplus
}
#endif
#endif
You will notice that the C++ API signature returns a jobject and accepts a Pointer to
JNIEnv, a reference to jobject (Java object) that called this C++ method and a reference
to jobject that contains Request data.
#include "com_test_JavaServer.h"
#include "string.h"
#include "stdlib.h"
https://medium.com/p/1378ca904202 4/16
17/5/23, 11:31 How to use JNI as a bridge between Java and C++ | by Sumit Datta | The Startup | Medium
return responseData;
}
#!/bin/sh
g++ -c -fPIC -fpermissive -I/usr/lib/jvm/java-11-openjdk-amd64/include
-I/usr/lib/jvm/java-11-openjdk-amd64/include/linux
calculate_volume_cplusplus.c -o calculate_volume_cplusplus.o
https://medium.com/p/1378ca904202 5/16
17/5/23, 11:31 How to use JNI as a bridge between Java and C++ | by Sumit Datta | The Startup | Medium
Keep both jar file and the .so file in the same directory (though not necessarily always)
and run the following command
You will see the Volume calculated by the C++ code being printed by Java class.
// Let's see what would have been the volume if it was calculated by
the Java program
printf("Let's see what would have been the volume if it was
calculated by the Java program\n");
printf("Start\n");
jclass javaServerClass = env->GetObjectClass(obj);
jmethodID methodCalculateVolumeInJava = env-
>GetMethodID(javaServerClass, "calculatevolume_in_java", "
(Lcom/test/RequestData;)Lcom/test/ResponseData;");
if (methodCalculateVolumeInJava == NULL) {
printf("calculatevolume_in_java method does not exists, so returning
\n");
return NULL;
}
responseData = env->CallObjectMethod(obj,
methodCalculateVolumeInJava, input);
if (methodGetVolume == NULL) {
printf("getVolume method does not exists, so returning \n");
return NULL;
}
jdouble javaVolume = (jdouble)env->CallDoubleMethod(responseData,
methodGetVolume);
printf ("Java Volume %f\n", javaVolume);
printf("End\n");
Jni
Follow
https://medium.com/p/1378ca904202 7/16
17/5/23, 11:31 How to use JNI as a bridge between Java and C++ | by Sumit Datta | The Startup | Medium
71
https://medium.com/p/1378ca904202 8/16
17/5/23, 11:31 How to use JNI as a bridge between Java and C++ | by Sumit Datta | The Startup | Medium
If You Want to Be a Creator, Delete All (But Two) Social Media Platforms
In October 2022, during the whole Elon Musk debacle, I finally deleted Twitter from my phone.
Around the same time, I also logged out of…
20K 371
https://medium.com/p/1378ca904202 9/16
17/5/23, 11:31 How to use JNI as a bridge between Java and C++ | by Sumit Datta | The Startup | Medium
Goodbye ChatGPT: Here Are (New) AI Tools That Will Blow Your Mind
I bet that 99% of the readers are not familiar with any of these tools.
6.6K 86
https://medium.com/p/1378ca904202 10/16
17/5/23, 11:31 How to use JNI as a bridge between Java and C++ | by Sumit Datta | The Startup | Medium
Sumit Datta
https://medium.com/p/1378ca904202 11/16
17/5/23, 11:31 How to use JNI as a bridge between Java and C++ | by Sumit Datta | The Startup | Medium
45 1
60
Lists
https://medium.com/p/1378ca904202 12/16
17/5/23, 11:31 How to use JNI as a bridge between Java and C++ | by Sumit Datta | The Startup | Medium
Staff Picks
303 stories · 65 saves
Self-Improvement 101
20 stories · 59 saves
Productivity 101
20 stories · 51 saves
David Marko
10
https://medium.com/p/1378ca904202 13/16
17/5/23, 11:31 How to use JNI as a bridge between Java and C++ | by Sumit Datta | The Startup | Medium
808 18
https://medium.com/p/1378ca904202 14/16
17/5/23, 11:31 How to use JNI as a bridge between Java and C++ | by Sumit Datta | The Startup | Medium
Burak in ITNEXT
115 3
1K 10
https://medium.com/p/1378ca904202 15/16
17/5/23, 11:31 How to use JNI as a bridge between Java and C++ | by Sumit Datta | The Startup | Medium
https://medium.com/p/1378ca904202 16/16