| Start | ||||||||
|---|---|---|---|---|---|---|---|---|
| Calling C++ methods from Java Static and member methods in Java can be marked as native to indicate that their implementation is to be found in a shared library file. Upon execution of a native method, the JVM looks for a corresponding function in loaded libraries (see Loading native libraries (opens new window)), using a simple name mangling scheme, performs argument conversion and stack setup, then hands over control to native code. Java codeC++ codeHeader files containing native function declarations should be generated using the ... produces the following header file (comments stripped for brevity) : Here is an example implementation : OutputRunning the example class above yields the following output : Invoked C++ 'printString' from Java |
||||||||
| Calling Java methods from C++ (callback) Calling a Java method from native code is a two-step process :
Java codeC++ codeOutputGot float from C++: 5.221 Getting the descriptorDescriptors (or internal type signatures) are obtained using the javap program on the compiled |
||||||||
| Loading native libraries The common idiom for loading shared library files in Java is the following : Calls to This allows to defer shared library loading until necessary, but requires extra care to avoid Target file lookupShared library files are searched for in the paths defined by the Watch out for system path separators : for example, Windows uses Note that An alternative to Parameters
RemarksSetting up JNI requires both a Java and a native compiler. Depending on the IDE and OS, there is some setting up required. A guide for Eclipse can be found here (opens new window). A full tutorial can be found here (opens new window). These are the steps for setting up the Java-C++ linkage on windows:
Callbacks (Calling Java methods from native code) requires to specify a method descriptor. If the descriptor is incorrect, a runtime error occurs. Because of this, it is helpful to have the descriptors made for us, this can be done with |
Read Article
https://develop.horje.com/learn/1434/reference