SlideShare a Scribd company logo
C++ programming
with JNI
What is JNI ?
 The Java Native Interface (JNI) is a native programming interface
that is part of the Java Software Development Kit (SDK).
 Through the use of JNI, Java code can use C++ code, C++ code
can call Java code, and a variety of other languages too.
 This tutorial deals with the most UNcommon applications of JNI:
calling Java code from C/C++ programs.
Why use JNI ?
JNI allows you to invoke Java class methods from within native code. Often, to
do this, you must create and initialize a JVM within the native code using the
Invocation API. The following are typical situations where you might decide to
call Java code from C/C++ code:
You want to implement platform-independent portions of code for functionality
that will be used across multiple platforms.
You have code or code libraries written in Java that you need to access in
native applications.
You want to take advantage of the standard Java class library from native
code.
Four Steps to Call Java from C++
Write the Java code. This step consists of writing the Java class or classes
that implement (or call other methods that implement) the functionality you want
to access.
Compile the Java code. The Java class or classes must be successfully
compiled to bytecode before they can be used.
Write the C/C++ code. This code will create and instantiate a JVM and call
the correct Java methods.
Run the native C/C++ application. We'll run the application to see if it works.
We'll also go over some tips for dealing with common errors.
Step 1: Write the Java code
We start by writing the Java source code file, which will implement the
functionality we want to make available to the native C/C++ code.
public class Sample2
{
public static int intMethod(int n) {
return n*n;
}
public static boolean booleanMethod(boolean bool) {
return !bool;
}
}
Sample2.java implements two static Java methods; intMethod(int n) and
booleanMethod(boolean bool).
My example shows static methods because is easier to call them. We do not
have to instantiate an object over JNI to invoke them (that can be done, but it’s
more complex).
Step 2: Compile the Java code
 Next, compile the Java
 Make a JAR.
 Place the JAR in your classpath **

** ( or cheat and put in ../Java/jre1.xx/lib/ext )
Step 3: Write the C/C++ code
All Java bytecode must be executed in a JVM, even when running in a native
application. So our C/C++ application must do the following;
1. Create a JVM
2. Lookup the Java class
3. Lookup the Java class method
4. Call the method
5. Release manually created Java resources (if any)
6. Trap exceptions, release them
We'll start with a look at the complete code for C++ applications.
#include <jni.h>
int main() {
JavaVMOption options[1];
JNIEnv *m_env;
JavaVM *m_java;
JavaVMInitArgs vm_args;
//constructor to create the JVM connection
JavaVMInitArgs vm_args;
JavaVMOption options[1];
long jvmStatus;
//================== prepare loading of Java VM ============================
options[0].optionString = "-Djava.class.path=. -Xms=50M -Xmx100M";
vm_args.version = JNI_VERSION_1_8; // minimum Java version
vm_args.nOptions = 1; // number of options
vm_args.options = options;
//=============== load and initialize Java VM and JNI interface =============
jvmStatus = JNI_CreateJavaVM(&m_java, (void**) &m_env, &vm_args);
Declare JVM variablesDeclare JVM variables
Set JVM optionsSet JVM options
Create the JVMCreate the JVM
if (status != JNI_ERR) {
jclass Sample2 = m_parser_env->FindClass("Sample2");
if (Sample2 != 0) {
jmethodID intID = m_env->GetStaticMethodID(Sample2, "intMethod", "(I)I");
jmethodID midID = m_env->GetStaticMethodID(Sample2, "booleanMethod", "(Z)Z");
if (intID != 0) {
int square = m_env->CallStaticIntMethod(Sample2, intID, 8);
cout<<"Result of intMethod: "<< square << endl;
}
if (midID != 0) {
bool not = m_env->CallStaticBooleanMethod( Sample2, midID, true);
cout<<"Result of booleanMethod: "<< not << endl;
}
}
}
m_env->DestroyJavaVM(m_java);
return 0;
} else
return -1;
}
Test for connectivityTest for connectivity
Lookup the Java classLookup the Java class
Lookup the class
method signatures
Lookup the class
method signatures
Call the methodCall the method
Native types supported by JNI
Native Java method parameter types are rendered, or mangled, into native code using
the encoding specified in the table below.
Java Type Code
boolean Z
byte B
char C
short S
int I
long J
float F
double D
void V
class Lclassname;
Java class and method signatures
When looking up Java classes and methods over JNI (prior to execution), the
signature needs to match.
TIP: use javap –s CLASSNAME.class to lookup your class signatures.
C:...parser>javap -s MYObject.class
Compiled from "MYObject.java"
public abstract class xxx.yyy.zzz.MYObject {
public static java.lang.String VERSION;
Signature: Ljava/lang/String;
public int getM_iVersion();
Signature: ()I
public void setM_iVersion(int);
Signature: (I)V
public boolean isM_bBroadcast();
Signature: ()Z
public void setM_bBroadcast(boolean);
Signature: (Z)V
public int getPrecBitcode(java.lang.String);
Signature: (Ljava/lang/String;)I
Java strings versus C strings
Java strings are stored as sequences of 16-bit Unicode characters,
while C strings are stored as sequences of 8-bit null-terminated
characters. JNI provides several useful functions for converting
between and manipulating Java strings and C strings. The code
snippet below demonstrates how to convert C strings to Java strings,
and back.
/* convert a C string to a Java String */
char[] str = "To be or not to be.n";
jstring jstr = m_env- >NewStringUTF( str);
/* convert a Java String to a C string */
const char* msg_str = m_env- >GetStringUTFChars(jstr , 0);
Local versus Global references
When programming with JNI, you will be required to use references to Java
objects. By default JNI creates local references to ensure that they are liable for
garbage collection.
//populate the MYObject, saving the jMYObject to use again
object jMYObject = popMyObject(theMsgBytes);
object jGlobalMYObject = jni_env->NewGlobalRef(jMYObject );
…
//release JNI objects for garbage collection
m_parser_env->DeleteLocalRef(jMYObject);
m_parser_env->DeleteGlobalRef(jGlobalMYObject);
Use Global References to prevent objects that you reuse from being garbage
collected.
* Be sure that you delete all your Global References, otherwise your Java objects will
never be free. Delete your Local References to force garbage collection of them.
Error handling
Using native methods in Java programs breaks the Java security model.
Because Java programs run in a controlled runtime system (the JVM), the
designers of the Java platform decided to help the programmer by checking
common runtime errors like array indices, out-of-bounds errors, and null pointer
errors.
After you make each JNI call, test for Java exceptions. Exceptions need to be
cleared, otherwise the JVM can bind up and your C++ application WILL
segfault.
//file bytes
const vector<byte>* bytesIn;
//convert the passed vector of bytes to jbyteArray
jbyteArray tmpjMsgBytes;
tmpjMsgBytes = m_env->NewByteArray(bytesIn->size());
if (m_env->ExceptionCheck())
throw JNI_ERROR;
m_env->SetByteArrayRegion(tmpjMsgBytes, 0, bytesIn->size(), (signed char*) &bytesIn->front());
if (m_env->ExceptionCheck())
throw JNI_ERROR;
Error handling 2
The JNI methods that detect exceptions, can return JThrowable objects. Use
this to lookup your Java exceptions and stack traces.
try {
//Do Stuff
tmpjMsgBytes = m_env->NewByteArray(bytesIn->size());
if (m_env->ExceptionCheck())
throw JNI_ERROR;
} Catch JNI_ERROR {
//JNI call to grab the exception object
jthrowable theException = m_env->ExceptionOccurred();
//clear the exception, if not the JVM processing halts
m_env->ExceptionClear();
//call method to query over JNI for the exception
string the_error = m_fetchJavaExceptionMessage( theException);
}
JNI Performance Tips
 Calling Java methods from C++ over JNI adds about 5-10ns per call.
 Querying for java classes and methods over JNI is expensive. Look them up
once, and put them in a map to use over and over again.
 Creating and destroying a JVM every time you interact with Java from native
code can waste resources and decrease performance.
 Your C++ application can only create one JVM. Manage it as any other
resource with RAII.
 The JVM is tunable even from unmanaged code. Most Java arguments can
be passed at creation time such as; Xms, Xmx, -Djava.class.path etc.
 Threading; JNI provides methods to synchronize blocks of code between C+
+ and Java.

More Related Content

What's hot

Java if and else
Java if and elseJava if and else
Java if and else
pratik8897
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
elliando dias
 

What's hot (20)

Java if and else
Java if and elseJava if and else
Java if and else
 
Java Persistence API
Java Persistence APIJava Persistence API
Java Persistence API
 
Java tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenJava tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo Cahersiveen
 
Learn Java Part 2
Learn Java Part 2Learn Java Part 2
Learn Java Part 2
 
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 1
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 1Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 1
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 1
 
Gdb cheat sheet
Gdb cheat sheetGdb cheat sheet
Gdb cheat sheet
 
An introduction to JVM performance
An introduction to JVM performanceAn introduction to JVM performance
An introduction to JVM performance
 
Bytecode manipulation with Javassist and ASM
Bytecode manipulation with Javassist and ASMBytecode manipulation with Javassist and ASM
Bytecode manipulation with Javassist and ASM
 
Core java concepts
Core java concepts Core java concepts
Core java concepts
 
Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)
 
Cheat Sheet java
Cheat Sheet javaCheat Sheet java
Cheat Sheet java
 
DIWE - Programming with JavaScript
DIWE - Programming with JavaScriptDIWE - Programming with JavaScript
DIWE - Programming with JavaScript
 
Pythia Reloaded: An Intelligent Unit Testing-Based Code Grader for Education
Pythia Reloaded: An Intelligent Unit Testing-Based Code Grader for EducationPythia Reloaded: An Intelligent Unit Testing-Based Code Grader for Education
Pythia Reloaded: An Intelligent Unit Testing-Based Code Grader for Education
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Javantura v2 - All Together Now - making Groovy and Scala sing together - Din...
Javantura v2 - All Together Now - making Groovy and Scala sing together - Din...Javantura v2 - All Together Now - making Groovy and Scala sing together - Din...
Javantura v2 - All Together Now - making Groovy and Scala sing together - Din...
 
Improving Android Performance at Droidcon UK 2014
Improving Android Performance at Droidcon UK 2014Improving Android Performance at Droidcon UK 2014
Improving Android Performance at Droidcon UK 2014
 
Javantura v2 - Making Java web-apps Groovy - Franjo Žilić
Javantura v2 - Making Java web-apps Groovy - Franjo ŽilićJavantura v2 - Making Java web-apps Groovy - Franjo Žilić
Javantura v2 - Making Java web-apps Groovy - Franjo Žilić
 
Checking the Cross-Platform Framework Cocos2d-x
Checking the Cross-Platform Framework Cocos2d-xChecking the Cross-Platform Framework Cocos2d-x
Checking the Cross-Platform Framework Cocos2d-x
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
 
AnyObject – 自分が見落としていた、基本の話
AnyObject – 自分が見落としていた、基本の話AnyObject – 自分が見落としていた、基本の話
AnyObject – 自分が見落としていた、基本の話
 

Similar to C++ programming with jni

Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
sanjeeviniindia1186
 

Similar to C++ programming with jni (20)

Using the Android Native Development Kit (NDK)
Using the Android Native Development Kit (NDK)Using the Android Native Development Kit (NDK)
Using the Android Native Development Kit (NDK)
 
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
 
Jvm internals
Jvm internalsJvm internals
Jvm internals
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)
 
Java Tutorial | My Heart
Java Tutorial | My HeartJava Tutorial | My Heart
Java Tutorial | My Heart
 
02 basic java programming and operators
02 basic java programming and operators02 basic java programming and operators
02 basic java programming and operators
 
Java tutorials
Java tutorialsJava tutorials
Java tutorials
 
Java tut1
Java tut1Java tut1
Java tut1
 
Java tut1
Java tut1Java tut1
Java tut1
 
Javatut1
Javatut1 Javatut1
Javatut1
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Java_Tutorial_Introduction_to_Core_java.ppt
Java_Tutorial_Introduction_to_Core_java.pptJava_Tutorial_Introduction_to_Core_java.ppt
Java_Tutorial_Introduction_to_Core_java.ppt
 
Android and cpp
Android and cppAndroid and cpp
Android and cpp
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 
Java tut1
Java tut1Java tut1
Java tut1
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Tutorial java
Tutorial javaTutorial java
Tutorial java
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 
Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Core Java introduction | Basics | free course
Core Java introduction | Basics | free course
 
Java
Java Java
Java
 

Recently uploaded

Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 

Recently uploaded (20)

Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
 
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting software
 
Breaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdfBreaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdf
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 

C++ programming with jni

  • 2. What is JNI ?  The Java Native Interface (JNI) is a native programming interface that is part of the Java Software Development Kit (SDK).  Through the use of JNI, Java code can use C++ code, C++ code can call Java code, and a variety of other languages too.  This tutorial deals with the most UNcommon applications of JNI: calling Java code from C/C++ programs.
  • 3. Why use JNI ? JNI allows you to invoke Java class methods from within native code. Often, to do this, you must create and initialize a JVM within the native code using the Invocation API. The following are typical situations where you might decide to call Java code from C/C++ code: You want to implement platform-independent portions of code for functionality that will be used across multiple platforms. You have code or code libraries written in Java that you need to access in native applications. You want to take advantage of the standard Java class library from native code.
  • 4. Four Steps to Call Java from C++ Write the Java code. This step consists of writing the Java class or classes that implement (or call other methods that implement) the functionality you want to access. Compile the Java code. The Java class or classes must be successfully compiled to bytecode before they can be used. Write the C/C++ code. This code will create and instantiate a JVM and call the correct Java methods. Run the native C/C++ application. We'll run the application to see if it works. We'll also go over some tips for dealing with common errors.
  • 5. Step 1: Write the Java code We start by writing the Java source code file, which will implement the functionality we want to make available to the native C/C++ code. public class Sample2 { public static int intMethod(int n) { return n*n; } public static boolean booleanMethod(boolean bool) { return !bool; } } Sample2.java implements two static Java methods; intMethod(int n) and booleanMethod(boolean bool). My example shows static methods because is easier to call them. We do not have to instantiate an object over JNI to invoke them (that can be done, but it’s more complex).
  • 6. Step 2: Compile the Java code  Next, compile the Java  Make a JAR.  Place the JAR in your classpath **  ** ( or cheat and put in ../Java/jre1.xx/lib/ext )
  • 7. Step 3: Write the C/C++ code All Java bytecode must be executed in a JVM, even when running in a native application. So our C/C++ application must do the following; 1. Create a JVM 2. Lookup the Java class 3. Lookup the Java class method 4. Call the method 5. Release manually created Java resources (if any) 6. Trap exceptions, release them We'll start with a look at the complete code for C++ applications.
  • 8. #include <jni.h> int main() { JavaVMOption options[1]; JNIEnv *m_env; JavaVM *m_java; JavaVMInitArgs vm_args; //constructor to create the JVM connection JavaVMInitArgs vm_args; JavaVMOption options[1]; long jvmStatus; //================== prepare loading of Java VM ============================ options[0].optionString = "-Djava.class.path=. -Xms=50M -Xmx100M"; vm_args.version = JNI_VERSION_1_8; // minimum Java version vm_args.nOptions = 1; // number of options vm_args.options = options; //=============== load and initialize Java VM and JNI interface ============= jvmStatus = JNI_CreateJavaVM(&m_java, (void**) &m_env, &vm_args); Declare JVM variablesDeclare JVM variables Set JVM optionsSet JVM options Create the JVMCreate the JVM
  • 9. if (status != JNI_ERR) { jclass Sample2 = m_parser_env->FindClass("Sample2"); if (Sample2 != 0) { jmethodID intID = m_env->GetStaticMethodID(Sample2, "intMethod", "(I)I"); jmethodID midID = m_env->GetStaticMethodID(Sample2, "booleanMethod", "(Z)Z"); if (intID != 0) { int square = m_env->CallStaticIntMethod(Sample2, intID, 8); cout<<"Result of intMethod: "<< square << endl; } if (midID != 0) { bool not = m_env->CallStaticBooleanMethod( Sample2, midID, true); cout<<"Result of booleanMethod: "<< not << endl; } } } m_env->DestroyJavaVM(m_java); return 0; } else return -1; } Test for connectivityTest for connectivity Lookup the Java classLookup the Java class Lookup the class method signatures Lookup the class method signatures Call the methodCall the method
  • 10. Native types supported by JNI Native Java method parameter types are rendered, or mangled, into native code using the encoding specified in the table below. Java Type Code boolean Z byte B char C short S int I long J float F double D void V class Lclassname;
  • 11. Java class and method signatures When looking up Java classes and methods over JNI (prior to execution), the signature needs to match. TIP: use javap –s CLASSNAME.class to lookup your class signatures. C:...parser>javap -s MYObject.class Compiled from "MYObject.java" public abstract class xxx.yyy.zzz.MYObject { public static java.lang.String VERSION; Signature: Ljava/lang/String; public int getM_iVersion(); Signature: ()I public void setM_iVersion(int); Signature: (I)V public boolean isM_bBroadcast(); Signature: ()Z public void setM_bBroadcast(boolean); Signature: (Z)V public int getPrecBitcode(java.lang.String); Signature: (Ljava/lang/String;)I
  • 12. Java strings versus C strings Java strings are stored as sequences of 16-bit Unicode characters, while C strings are stored as sequences of 8-bit null-terminated characters. JNI provides several useful functions for converting between and manipulating Java strings and C strings. The code snippet below demonstrates how to convert C strings to Java strings, and back. /* convert a C string to a Java String */ char[] str = "To be or not to be.n"; jstring jstr = m_env- >NewStringUTF( str); /* convert a Java String to a C string */ const char* msg_str = m_env- >GetStringUTFChars(jstr , 0);
  • 13. Local versus Global references When programming with JNI, you will be required to use references to Java objects. By default JNI creates local references to ensure that they are liable for garbage collection. //populate the MYObject, saving the jMYObject to use again object jMYObject = popMyObject(theMsgBytes); object jGlobalMYObject = jni_env->NewGlobalRef(jMYObject ); … //release JNI objects for garbage collection m_parser_env->DeleteLocalRef(jMYObject); m_parser_env->DeleteGlobalRef(jGlobalMYObject); Use Global References to prevent objects that you reuse from being garbage collected. * Be sure that you delete all your Global References, otherwise your Java objects will never be free. Delete your Local References to force garbage collection of them.
  • 14. Error handling Using native methods in Java programs breaks the Java security model. Because Java programs run in a controlled runtime system (the JVM), the designers of the Java platform decided to help the programmer by checking common runtime errors like array indices, out-of-bounds errors, and null pointer errors. After you make each JNI call, test for Java exceptions. Exceptions need to be cleared, otherwise the JVM can bind up and your C++ application WILL segfault. //file bytes const vector<byte>* bytesIn; //convert the passed vector of bytes to jbyteArray jbyteArray tmpjMsgBytes; tmpjMsgBytes = m_env->NewByteArray(bytesIn->size()); if (m_env->ExceptionCheck()) throw JNI_ERROR; m_env->SetByteArrayRegion(tmpjMsgBytes, 0, bytesIn->size(), (signed char*) &bytesIn->front()); if (m_env->ExceptionCheck()) throw JNI_ERROR;
  • 15. Error handling 2 The JNI methods that detect exceptions, can return JThrowable objects. Use this to lookup your Java exceptions and stack traces. try { //Do Stuff tmpjMsgBytes = m_env->NewByteArray(bytesIn->size()); if (m_env->ExceptionCheck()) throw JNI_ERROR; } Catch JNI_ERROR { //JNI call to grab the exception object jthrowable theException = m_env->ExceptionOccurred(); //clear the exception, if not the JVM processing halts m_env->ExceptionClear(); //call method to query over JNI for the exception string the_error = m_fetchJavaExceptionMessage( theException); }
  • 16. JNI Performance Tips  Calling Java methods from C++ over JNI adds about 5-10ns per call.  Querying for java classes and methods over JNI is expensive. Look them up once, and put them in a map to use over and over again.  Creating and destroying a JVM every time you interact with Java from native code can waste resources and decrease performance.  Your C++ application can only create one JVM. Manage it as any other resource with RAII.  The JVM is tunable even from unmanaged code. Most Java arguments can be passed at creation time such as; Xms, Xmx, -Djava.class.path etc.  Threading; JNI provides methods to synchronize blocks of code between C+ + and Java.